C++;带有类模板的未解析外部符号 这是C++中使用类模板的一个简单例子。这个代码有效 #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; } #包括 使用名称空间std; 样板 课堂测试{ 公众: Test(); 作废打印(); 私人: int i; }; 样板 Test::Test(){ i=1; cout
模板不能像任何其他源文件一样编译。接口和实现都应该位于头文件中(尽管有些人将它们拆分为C++;带有类模板的未解析外部符号 这是C++中使用类模板的一个简单例子。这个代码有效 #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; } #包括 使用名称空间std; 样板 课堂测试{ 公众: Test(); 作废打印(); 私人: int i; }; 样板 Test::Test(){ i=1; cout,c++,class,templates,external,C++,Class,Templates,External,模板不能像任何其他源文件一样编译。接口和实现都应该位于头文件中(尽管有些人将它们拆分为.hpp文件用于接口和.ipp文件用于实现,然后在.hpp文件末尾包含.ipp文件) 编译模板类时,编译器如何知道要生成哪些类?“所以我搜索了很多关于未解析外部符号的信息,但在这种情况下再也找不到了。”真的吗?简单的谷歌搜索“模板未解析外部符号”就可以给出准确的解决方案。 //Test.h #include <iostream> using namespace std; template <
.hpp
文件用于接口和.ipp
文件用于实现,然后在.hpp
文件末尾包含.ipp
文件)
编译模板类时,编译器如何知道要生成哪些类?“所以我搜索了很多关于未解析外部符号的信息,但在这种情况下再也找不到了。”真的吗?简单的谷歌搜索“模板未解析外部符号”就可以给出准确的解决方案。
//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;
}
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