Jump to content
  • Advertisement
Sign in to follow this  
Steve00000

templates

This topic is 3473 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

im trying to implement the use of templates in my project, in a class based enviroment. when i try to run my project i get this message error LNK2019: unresolved external symbol "public: void __thiscall FilerCls::WriteToFile<char>(char *,char)" (??$WriteToFile@D@FilerCls@@QAEXPADD@Z) referenced in function "public: void __thiscall GameClass::BeginProc(void)" (?BeginProc@GameClass@@QAEXXZ) i have the template declared in both my header and my source. so im wondering if anyone can help me i'd really appreciate it and for further info here's an example of the implementation //prototype in class in header template<class T>void WriteToFile(char* FileName, T Data); //description in seperate source, including the header void FilerCls::WriteToFile(char* FileName, T Data) { std::ofstream ofile; ofile.open(FileName); ofile << Data; ofile.close(); if(ofile.is_open()) ofile.close(); return; } and in another source including the header is implementation, thx anyone who helps

Share this post


Link to post
Share on other sites
Advertisement
Tragically, templates run rough-shod over the C++ compiling/linking paradigm. The short answer is you cannot put template function bodies in a CPP file and use them from other CPP files. The longer answer is here.

Share this post


Link to post
Share on other sites
You should take a look at this. Basically, it comes down to this; a template class must be defined in the same file in which it is declared (or to be more technically correct, in the same translation unit). What many people end up doing is writing the definition in the header, and the #including the .cpp file at the bottom of the header. The problem you encountered is that the definition is in a different translation unit from the declaration and so the compiler can't find it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!