//头文件
#ifndef Test_h__
#define Test_h__
template<class T>
class Test1
{
public:
static T Add(const T& x, const T& y);
};
#endif // Test_h__
//cpp文件
#include "Test.h"
template<class T>
T Test1<T>::Add(const T& x, const T& y)
{
return x + y;
}
//使用地方
Test1<int>::Add(1, 2);
以上编译会出现,链接问题,原因是,类模板必须显示初始化,也就是告诉具体类型
解决办法:
//修改后的cpp文件
#include "Test.h"
template<class T>
T Test1<T>::Add(const T& x, const T& y)
{
return x + y;
}
//要声明一次
template
int Test1<int>::Add(const int& x, const int& y);
顺便编译通过!
本文发布于:2024-01-31 06:12:38,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170665275926112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |