首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类模板cpp,hpp,cpp

类模板cpp,hpp,cpp
EN

Stack Overflow用户
提问于 2015-09-03 03:30:02
回答 1查看 546关注 0票数 0

这个类在我的.hpp文件中

代码语言:javascript
复制
template<class T = int>
class Matrix
{
    public:
    Matrix();
}

我有这个Matrix.cpp文件

代码语言:javascript
复制
#include "Matrix.hpp"
template<class T>
Matrix<T>::Matrix()
{
    vector<T> vecN(1, 0);
    _matrix.resize(1, vecN);
    _rows = 1;
    _cols = 1;
}

但是当添加主程序时,它不会工作

代码语言:javascript
复制
#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)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-03 03:33:06

模板代码必须在标头中,除非是专门化的。

这是因为模板用于在使用时生成实际的类。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32366494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档