error LNK2019: 无法解析的外部符号 ),该符号在函数 _main 中被引用

开开心心 2011-07-11 09:23:00
问题: error LNK2019: 无法解析的外部符号 "public: __thiscall Compare<char>::Compare<char>(char,char)" (??0?$Compare@D@@QAE@DD@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: float __thiscall Compare<float>::min(void)" (?min@?$Compare@M@@QAEMXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: float __thiscall Compare<float>::max(void)" (?max@?$Compare@M@@QAEMXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Compare<float>::Compare<float>(float,float)" (??0?$Compare@M@@QAE@MM@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall Compare<int>::min(void)" (?min@?$Compare@H@@QAEHXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall Compare<int>::max(void)" (?max@?$Compare@H@@QAEHXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Compare<int>::Compare<int>(int,int)" (??0?$Compare@H@@QAE@HH@Z),该符号在函数 _main 中被引用
1>D:\vc++2005\程序\9-14\Debug\9-14.exe : fatal error LNK1120: 9 个无法解析的外部命令

主函数文件:
#include <iostream>

#include "Compare.h"
using namespace std;

int main()
{
Compare<int> cmp1(3,7);
cout << cmp1.max() << " is the Maximum of two inteder numbers." << endl;
cout<< cmp1.min() << " is the Minimum of two inteder numbers." << endl << endl;
Compare<float> cmp2(45.78,93.6);
cout << cmp2.max() << " is the Maximum of two float numbers." << endl;
cout << cmp2.min() << " is the Minimum of two float numbers." << endl << endl;
Compare<char> cmp3('a','A');
cout << cmp3.max() << " is the Maximum of two characters." << endl;
cout << cmp3.min() << " is the Minimum of two characters." << endl;
return 0;
}
头文件声明的类:
#pragma once
#include <iostream>
using namespace std;

template <class T>
class Compare
{
public:
Compare(T a, T b);
public:
T max(void);
T min(void);
private:
T x,y;
};
类的定义:
#include <iostream>
using namespace std;
#include "Compare.h"

template <class T>
Compare<T>::Compare(T a, T b)
{
x = a;
y = b;
}

template <class T>
T Compare<T>::max(void)
{
return (x > y) ? x:y;
}

template <class T>
T Compare<T>::min(void)
{
return (x < y) ? x:y;
}
...全文
2806 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hedy007 2011-07-11
  • 打赏
  • 举报
回复

http://blog.csdn.net/ljfxmf/article/details/2510786

希望能帮到lz
至善者善之敌 2011-07-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kkxxyyp 的回复:]
将Compare.cpp的代码全放到Compare.h里去是可以运行,可是老师规定类的声明和定义必须分开,他说这么写才规范,怎么办啊???????
[/Quote]

在c++中定义的模板类,及其实现要放在一起。不光是模板类是这样,模板函数也是如此。 现在想想可能是模板函数或模板类本身不能直接生成代码,而必须和特定的数据类型结合后才能生成代码的缘故吧。而CPP文件一般是要生成OBJ文件的。应用了模板的函数即使看起来似乎已经实现了,但实际上塔仍然属于一种声明。如下例:
template <class T>
int compare(T t1,T t2){
{
if(t1>t2) return 1;
if(t1==t2) return 0;
if(t1<t2)return -1;
}
个人觉得这也只是一个声明,在编译器进行编译的时候并不能立刻生成obj
又如:
在头文件 A.h
template <class T>
class A(){
int compare(T t1,T t2);
}
在cpp A.cpp中
template <class T>

int A:: compare(T t1,T t2)
{
if(t1>t2) return 1;
if(t1==t2) return 0;
if(t1<t2)return -1;
}
编译器一般是通不过的。在编译的时候,cpp文件需要生成.o文件,而模板类在其实现时是未有办法生成.o文件的。一个方法就是把模板类的实现直接放在.h文件中。并且在某些编译器中还必需在声明时就实现才能通过 如下所示:
//A.hpp
template <class T>
class A{
int compare(T t1,T t2)
{
if(t1>t2) return 1;
if(t1==t2) return 0;
if(t1<t2)return -1;
}
}
}
ryfdizuo 2011-07-11
  • 打赏
  • 举报
回复
#pragma once
#include <iostream>
using namespace std;

template <class T>
class Compare
{
public:
Compare(T a, T b);
public:
T max(void);
T min(void);
private:
T x,y;
};

#include "Compare.cpp" //这里引用。。。


Compare.cpp中去掉#include "Compare.h"
开开心心 2011-07-11
  • 打赏
  • 举报
回复
将Compare.cpp的代码全放到Compare.h里去是可以运行,可是老师规定类的声明和定义必须分开,他说这么写才规范,怎么办啊???????
luciferisnotsatan 2011-07-11
  • 打赏
  • 举报
回复
把Compare.cpp的代码全放到Compare.h里去
ryfdizuo 2011-07-11
  • 打赏
  • 举报
回复
template <class T>
Compare<T>::Compare(T a, T b)
{
x = a;
y = b;
}

template <class T>
T Compare<T>::max(void)
{
return (x > y) ? x:y;
}

template <class T>
T Compare<T>::min(void)
{
return (x < y) ? x:y;
}

把这些都放到Compare.h文件中就可以了。
ryfdizuo 2011-07-11
  • 打赏
  • 举报
回复
编译器不支持模板分离模型,

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧