template<class numtype>
class Compare
{
public:
Compare(numtype,numtype);
numtype max();
private:
numtype x,y;
};
// #include”compare.cpp”
#include”compare.h”
template<class numtype>
Compare<numtype>::Compare(numtype a,numtype b):x(a),y(b){}
template<class numtype>
numtype Compare<numtype>::max()
{
return (x>y)?x:y;
}
// #include<main.cpp>
#include<iostream>
#include”compare.h”
using namespace std;
int main()
{
Compare<int> c(3,7);
cout<<“最大值:”<<c.max()<<endl;
return 0;
}
总提示次错误:Linking…
main.obj : error LNK2001: unresolved external symbol “public: int __thiscall Compare<int>::max(void)” (?max@?$Compare@H@@QAEHXZ)
main.obj : error LNK2001: unresolved external symbol “public: __thiscall Compare<int>::Compare<int>(int,int)” (??0?$Compare@H@@QAE@HH@Z)
Debug/5.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.
具体原因参看:http://blog.csdn.net/pongba/article/details/19130