By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

template linker error

P: n/a
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?

Sep 26 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Gr*****@nospam.com wrote:
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

Sep 26 '05 #2

P: n/a
Gr*****@nospam.com wrote:
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
Sep 26 '05 #3

P: n/a

Gr*****@nospam.com wrote:
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

Sep 26 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.