C++ Unresolved external symbol with Class templates [duplicate]

混江龙づ霸主 提交于 2019-11-27 08:54:01

问题


This question already has an answer here:

  • Why can templates only be implemented in the header file? 16 answers
  • What is an undefined reference/unresolved external symbol error and how do I fix it? 32 answers

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:


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?



来源:https://stackoverflow.com/questions/20330521/c-unresolved-external-symbol-with-class-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!