Jump to content
  • Advertisement
Sign in to follow this  
Triad_prague

weird error :(

This topic is 2603 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, I tried to put my template class definition in separate cpp file, but the compiler threw me some errors :(
this is my sample files

test.h

#ifndef _TEST
#define _TEST

template <class T>
class res_mgr
{
public:
res_mgr();
~res_mgr();

int load_all();
};

#endif


test.cpp

#include "test.h"

template<class T>
res_mgr<T>::res_mgr()
{
}

template<class T>
res_mgr<T>::~res_mgr()
{
}

template<class T>
int res_mgr<T>::load_all()
{
return 0;
}


main.cpp

#include "test.h"

int main(int argc, char** argv)
{
res_mgr<int> mgr;
return 0;
}


I compiled the project, and I got something like these:
[color="#0000FF"]--------------------Configuration: cpp_test - Win32 Debug--------------------
Compiling...
main.cpp
test.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: __thiscall res_mgr<int>::~res_mgr<int>(void)" (??1?$res_mgr@H@@QAE@XZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall res_mgr<int>::res_mgr<int>(void)" (??0?$res_mgr@H@@QAE@XZ)
Debug/cpp_test.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

cpp_test.exe - 3 error(s), 0 warning(s)

[color="#000000"]EDIT:
[color="#000000"]I've googled to look up some c++ template tutorials so I can compare my code with theirs, but it seems that my code is just fine. I don't understand what caused the errors, can you guys shed some lights on this?

Btw I use the oldie MSVC6.0

Share this post


Link to post
Share on other sites
Advertisement
The short answer is that template definition can't go into separate source files without special compiler support that MSVC doesn't have. You need to put the template definitions in the header.

Share this post


Link to post
Share on other sites

The short answer is that template definition can't go into separate source files without special compiler support that MSVC doesn't have. You need to put the template definitions in the header.


you're right, I moved all the implementations to the .h file, and it compiled just fine. Hmm so MSVC is the real problem...once again thanks :lol:

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!