LNK2019 Errors with Templates

C

Chris B

Im trying out VS.NET 2003 by trying to make my own single linked list
library. Im trying to use templates but keep getting LNK2019 errors.
Right now I slimmed down my project to just the main.cpp, linked
list.cpp/h and Node.cpp/h
My error messages:
LibTest error LNK2019: unresolved external symbol "public: __thiscall
CSL::SingleLinkedList<int>::~SingleLinkedList<int>(void)"
(??1?$SingleLinkedList@H@CSL@@QAE@XZ) referenced in function _main

LibTest error LNK2019: unresolved external symbol "public: void
__thiscall CSL::SingleLinkedList<int>::Reverse(void)"
(?Reverse@?$SingleLinkedList@H@CSL@@QAEXXZ) referenced in function
_main


I posted the basics of my code at this link:
http://www.rafb.net/paste/results/OE4JNE25.html
I left out some to save space

I'm looking for any suggestions.
 
C

Carl Daniel [VC++ MVP]

Chris said:
I'm looking for any suggestions.

Have you by chance put the bodies of your template class members in a .CPP
file, like you would an ordinary non-template class? If so, that's your
problem - the bodies have to be visible to the compiler in every translation
unit where the template is used. In practice this usually means that
templates are implemented entirely in the header file.

-cd
 
C

Chris B

Carl Daniel said:
Have you by chance put the bodies of your template class members in a .CPP
file, like you would an ordinary non-template class? If so, that's your
problem - the bodies have to be visible to the compiler in every translation
unit where the template is used. In practice this usually means that
templates are implemented entirely in the header file.

-cd

Thanks! I got it too work with everything in the .h file.
Is there any way to hide the implementation so that if everything is
not in the .h file?
 
C

Carl Daniel [VC++ MVP]

Chris said:
Thanks! I got it too work with everything in the .h file.
Is there any way to hide the implementation so that if everything is
not in the .h file?

No, not really. some people put the implementation in another file
(frequently with the extension .ipp) and then #include that file at the end
of the header, but in the end, that's really not much different from having
the implementation in the header.

The C++ standard provides for another template model through the 'export'
keyword that does allow you to keep your template function bodies out of the
header file. Unfortunately, only one C++ compiler in existence (Comeau C++)
fully supports export. With an export-capable compiler, you still have to
ship your templates as source code if you distribute your code... but it
does get the code out of the header file.

-cd
 

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

Top