这个类在我的.hpp文件中
template<class T = int>
class Matrix
{
public:
Matrix();
}我有这个Matrix.cpp文件
#include "Matrix.hpp"
template<class T>
Matrix<T>::Matrix()
{
vector<T> vecN(1, 0);
_matrix.resize(1, vecN);
_rows = 1;
_cols = 1;
}但是当添加主程序时,它不会工作
#include "Matrix.hpp"
int main(int argc, char** argv)
{
Matrix<int> test();
return 0;
}我听到一个很奇怪的错误
main.cpp:19: undefined reference to Matrix<int>::Matrix(unsigned int, unsigned int)' main.cpp:19:(.text+0x2d): relocation truncated to fit: R_X86_64_PC32 against undefined symbol Matrix<int>::Matrix(unsigned int, unsigned int)
发布于 2015-09-03 03:33:06
模板代码必须在标头中,除非是专门化的。
这是因为模板用于在使用时生成实际的类。
https://stackoverflow.com/questions/32366494
复制相似问题