大家能不能帮我看一下这个类模板错在哪里啊?
template.h:
template <typename numtype>
class Compare
{
public:
Compare(numtype a,numtype b);
numtype max(void);
numtype min(void);
private:
numtype x,y;
};
template.cpp:
#include "template.h"
template <typename numtype>
Compare<typename numtype> :: Compare(numtype a,numtype b)
{
x = a;
y = b;
}
template <typename numtype>
numtype Compare<typename numtype> :: max()
{
return (x > y)?x:y;
}
template <typename numtype>
numtype Compare<typename numtype> :: min()
{
return (x>y)?y:x;
}
main_1.cpp
#include <iostream>
using namespace std;
#include "template.h"
int main()
{
Compare<int> cmp1(3,7);
//cout << cmp1.max() << cout;
return 0;
}
VS2008编译提示:
1>------ 已启动全部重新生成: 项目: template, 配置: Debug Win32 ------
1>正在删除项目“template”(配置“Debug|Win32”)的中间文件和输出文件
1>正在编译...
1>template.cpp
1>main_1.cpp
1>正在生成代码...
1>正在链接...
1>template.obj : warning LNK4075: 忽略“/EDITANDCONTINUE”(由于“/INCREMENTAL:NO”规范)
1>main_1.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Compare<int>::Compare<int>(int,int)" (??0?$Compare@H@@QAE@HH@Z),该符号在函数 _main 中被引用
1>D:\c++\template\Debug\template.exe : fatal error LNK1120: 1 个无法解析的外部命令
1>生成日志保存在“file://d:\c++\template\template\Debug\BuildLog.htm”
1>template - 2 个错误,1 个警告
========== 全部重新生成: 成功 0 个,失败 1 个,跳过 0 个 ==========
[解决办法]
首先呢,告诉你,模板不支持分离编译:就是所有的定义实现都放在一个文件里。分开就错了。