weird error :(

Started by
6 comments, last by Triad_prague 12 years, 5 months ago
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
the hardest part is the beginning...
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.

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:
the hardest part is the beginning...

Hmm so MSVC is the real problem...

Not exactly...

[quote name='Triad_prague' timestamp='1319900347' post='4878256']
Hmm so MSVC is the real problem...

Not exactly...
[/quote]

hmm it is, perhaps...
the hardest part is the beginning...
In which case...


Hmm so MSVC6 is the real problem...


FIFY. :P

I imagine you'll continue hitting little roadblocks such as this until you upgrade to a decent compiler from this century. Unless there's a mission-critical reason to use MSVC6, you should get Microsoft Visual Studio 2010 Express for free.

<snip>

hmm it is, perhaps...


Yes, VC6 has a boatload of issues. But these (the problem linked and the one in this thread) aren't the same problem. Very, very few compilers supported 'export'.
[TheUnbeliever]
Okay, it's noted. I'm downloading vc2010express, altho it takes like 10 hours to download. Gonna try it later... thanks for the response guys :lol:
the hardest part is the beginning...

This topic is closed to new replies.

Advertisement