Jump to content
  • Advertisement

Archived

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

friman

c++ strange linking with templates

This topic is 5535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
.....
readFile(mdata);
  
************** 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
Advertisement
template functions should be put in header-files (.h)



Update GameDev.net system time campaign - success at last

Share this post


Link to post
Share on other sites
dalleboy you solved my problem,
thank-you
and again thak you, I could spend more void hours
I didn''t know that

Share this post


Link to post
Share on other sites
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!