template linker error

G

Grahamo

Hi,

I have a question that pertains to Templates and link time. This is
totally for my own understanding and to correct what's obviously an
erroneous view of things on my behalf;

Lets say I have;

// main.cpp

template <classT>
class some_template
{

... details

};

int main(int argc, char** argv)
{
some_template<some_user_defined_type> goo;
}


now when I compile main.cpp the compiler is going to see that it needs
to generate an instance of some_template for some_user_defined_type".
It will then go ahead and compile up this generated code into a .obj
which can later be used at link time.

How can it be at link time I can get a link error for
"some_template<user_defined_type>::~some_template<user_defined_type> "
? I mean the compiler has already generated and compiled it up into the
object code and that is to be used by the linker. Assuming the linker
is using the correct .obj etc. what's missing that link errors can
arise like that? I would have thought that everything was generated by
the compiler. Or rather what's wrong with my understanding?
 
M

mlimber

Hi,

I have a question that pertains to Templates and link time. This is
totally for my own understanding and to correct what's obviously an
erroneous view of things on my behalf;

Lets say I have;

// main.cpp

template <classT>
class some_template
{

... details

};

int main(int argc, char** argv)
{
some_template<some_user_defined_type> goo;
}


now when I compile main.cpp the compiler is going to see that it needs
to generate an instance of some_template for some_user_defined_type".
It will then go ahead and compile up this generated code into a .obj
which can later be used at link time.

How can it be at link time I can get a link error for
"some_template<user_defined_type>::~some_template<user_defined_type> "
? I mean the compiler has already generated and compiled it up into the
object code and that is to be used by the linker. Assuming the linker
is using the correct .obj etc. what's missing that link errors can
arise like that? I would have thought that everything was generated by
the compiler. Or rather what's wrong with my understanding?

Likely what's wrong is that you specified that there would be a
destructor for some_template<> but didn't actually supply the code for
it. The same would happen with a non-template class:

class A { ~A(); };
int main() { A a; return 0; } // Error: no A::~A()

If it's not that, then post a compilable sample of code that
demonstrates the problem.

Cheers! --M
 
J

John Harrison

Hi,

I have a question that pertains to Templates and link time. This is
totally for my own understanding and to correct what's obviously an
erroneous view of things on my behalf;

Lets say I have;

// main.cpp

template <classT>
class some_template
{

... details

};

int main(int argc, char** argv)
{
some_template<some_user_defined_type> goo;
}


now when I compile main.cpp the compiler is going to see that it needs
to generate an instance of some_template for some_user_defined_type".
It will then go ahead and compile up this generated code into a .obj
which can later be used at link time.

I guess that is an unwarranted assumption. Sure the compiler is going to
generate code for some_template<some_user_defined_type> and sure that
code is going to be in the object file, but I don't think there is any
requirement that code be usable at link time by other object files
(which I think it what you are talking about).

You might try placing this

template class some_template<some_user_defined_type>;

at file scope in main.cpp. It's called an explicit instantiation and
maybe it will achieve what you want.

Personally I just put template code in header files.

john
 
G

Greg

Hi,

I have a question that pertains to Templates and link time. This is
totally for my own understanding and to correct what's obviously an
erroneous view of things on my behalf;

Lets say I have;

// main.cpp

template <classT>
class some_template
{

... details

};

int main(int argc, char** argv)
{
some_template<some_user_defined_type> goo;
}


now when I compile main.cpp the compiler is going to see that it needs
to generate an instance of some_template for some_user_defined_type".
It will then go ahead and compile up this generated code into a .obj
which can later be used at link time.

How can it be at link time I can get a link error for
"some_template<user_defined_type>::~some_template<user_defined_type> "
? I mean the compiler has already generated and compiled it up into the
object code and that is to be used by the linker. Assuming the linker
is using the correct .obj etc. what's missing that link errors can
arise like that? I would have thought that everything was generated by
the compiler. Or rather what's wrong with my understanding?
From your example, it appears that the template's implementation
resides in a source (.cpp) file.

Templates should be completely implemented in header files. The
compiler needs to have seen a template definition before it compiles
any source files that use the template. Doing so ensures that all the
templates are instantiated by the time that the program is linked. So
making sure that all template code resides in header files could well
fix your problem.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top