C++ Unresolved external symbol with Class template

2019-01-20 20:58发布

Here's a simple example of using class templates in C++. This code works.

#include <iostream>

using namespace std;

template <class T>
class Test {
public:
   Test();
   void print();
private:
    int i;
};

template <class T>
Test<T>::Test() {
    i=1;
    cout<<"New instance"<<endl;
}

template <class T>
void Test<T>::print() {
    cout<<"Test"<<endl;
}

int main() {
    Test<int> i;
    i.print();
    return 0;
}

So when I separate this code in 3 files: main.cpp, Test.h, Test.cpp:

//Test.h
#include <iostream>

using namespace std;

template <class T>
class Test {
public:
   Test();
   void print();
private:
    int i;
};

//Test.cpp
#include "Test.h"

template <class T>
Test<T>::Test() {
    i=1;
    cout<<"New instance"<<endl;
}

template <class T>
void Test<T>::print() {
    cout<<"Test"<<endl;
}

//main.cpp
#include "Test.h"

using namespace std;

int main() {
    Test<int> i;
    i.print();
    return 0;
}

I get an errors:

1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall Test<int>::print(void)" (?print@?$Test@H@@QAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Test<int>::Test<int>(void)" (??0?$Test@H@@QAE@XZ) referenced in function _mai
1>C:\Programming\C++\stuff\templass\Debug\templass.exe : fatal error LNK1120: 2 unresolved externals

I use Microsoft Visual C++ 2010 Express. So I searched a lot about unresolved external symbol but didn't find anymore for this case. So what's my mistake?

1条回答
闹够了就滚
2楼-- · 2019-01-20 21:06

Templates cannot be compiled like any other source file. Both interface and implementation should live in the header file (although some split them in .hpp files for interface and .ipp files for implementation, and then include the .ipp file at the end of the .hpp file).

How would the compiler know what classes to generate when the template class is compiled?

查看更多
登录 后发表回答