This topic is now archived and is closed to further replies.


c++ strange linking with templates

Recommended Posts

friman    122
hi this is about c++ strange linking and compilation phenomenon. I created a general algorythm function in c++: ******** a.cpp ************
template <class T>
void readFile(T general)
{ ...... 
general.print(); }
************************* As you can see, the function takes an object of type T and calls the print() function of that object. One weird thing is that even if I change the line general.print(); to be stupid.print(); although there is no 'stupid' object defined, still the compilation of the function succeeds. the function is called from main: ******** main.cpp ***********
#indlude "data.h"
data mdata;
template <class T>
void readFile(T general);
************** ok, so the compilation works fine, but not the linking: error LNK2001: unresolved external symbol.... is created I would really apprise it, could you explain me how overcome this weirdness ? [edited by - friman on April 29, 2003 5:28:54 AM]

Share this post

Link to post
Share on other sites
petewood    819
template definitions could be put in cpp files if compilers supported it. However, most compilers don''t support it yet. You''ll have to keep the template defintions in the header if your compiler is MSVC++.

If it was in a cpp file you''d have to use the ''export'' keyword. Only comeau currently supports it.

Share this post

Link to post
Share on other sites