unresolved external symbol错误fatal error LNK1120

C++语言 码拜 9年前 (2015-10-24) 958次浏览
//  #include”compare.h”
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 时出错.

解决方案:40分
把compare.h compare.cpp两个文件合成一个文件:.cpp中的内容放在.h中去
解决方案:60分
模板类不支持分离编译,所以你需要全部放到一个文件中
具体原因参看:http://blog.csdn.net/pongba/article/details/19130

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明unresolved external symbol错误fatal error LNK1120
喜欢 (0)
[1034331897@qq.com]
分享 (0)