我在我的 C++ 程序中调用了一个模板:
const matrix::CMatrix<double> M1(3,3,{{5.0,0.0,2.0},{1.0,1.0,3.0},{6.0,7.0,7.0}});
我收到这样的链接器错误:
tests.h:15: undefined reference to `matrix::CMatrix<double>::CMatrix(int, int, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >)'
经过大量搜索,最后我意识到这是因为我已经将我的 h 和 cpp 文件分开了template
不容忍它。在 C 语言中,建议将 c 和 h 文件分开并使用 Makefile。但是根据c++中的此类问题,是否仍然建议将cpp和h文件分开?在 h 文件中实现一些模板并在 cpp 文件中实现一些其他功能并不是一种干净的方法。我应该怎么办?
edit:
matrix.h
template<typename T>
class CMatrix
{
.....
矩阵.cpp
implementation of CMatrix
main.cpp
defining M1