连接失败~!~何解?

liaojiededepan 2008-07-08 03:46:35
为什么会连接出问题?编译全ok~!
//////////mb.h///////
#ifndef _mb
#define _mb
#include<iostream>
using namespace std;
template <class T>
class Compare
{public:
Compare(T a,T b);
T max();
T min();
private:
T x,y;


};
#endif
/////////////hs.cpp/////////
#include "mb.h"
template <class T>
Compare <T>::Compare(T a,T b)
{x=a;
y=b;

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

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

}
/////////main.cpp/////////
#include "mb.h"
int main()
{Compare<float> c1(3.2,5.3);
cout<<c1.max()<<c1.min();
return 0;
}
/////////////////////////////
连接时
ompiling...
hs.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: float __thiscall Compare<float>::max(void)" (?max@?$Compare@M@@QAEMXZ)
main.obj : error LNK2001: unresolved external symbol "public: float __thiscall Compare<float>::min(void)" (?min@?$Compare@M@@QAEMXZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall Compare<float>::Compare<float>(float,float)" (??0?$Compare@M@@QAE@MM@Z)
Debug/第12题.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

第12题.exe - 4 error(s), 0 warning(s)

...全文
68 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hai040 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xkyx_cn 的回复:]
此警告可以不管

引用 3 楼 liaojiededepan 的回复:
1楼兄弟;
我把hs.cpp放到mb.h里侯
main.cpp编译有警告
提示:Compiling...
main.cpp
D:\程序\vc6.0组\我的工作空间\第六题\第12题\main.cpp(3) : warning C4305: 'argument' : truncation from 'const double' to 'float'
D:\程序\vc6.0组\我的工作空间\第六题\第12题\main.cpp(3) : warning C4305: 'argument' : truncation from 'const double' to 'float'

[/Quote]
不管也行,warning毕竟只是warning
hai040 2008-07-08
  • 打赏
  • 举报
回复
Compare <float> c1(3.2f,5.3f);
xkyx_cn 2008-07-08
  • 打赏
  • 举报
回复
此警告可以不管

[Quote=引用 3 楼 liaojiededepan 的回复:]
1楼兄弟;
我把hs.cpp放到mb.h里侯
main.cpp编译有警告
提示:Compiling...
main.cpp
D:\程序\vc6.0组\我的工作空间\第六题\第12题\main.cpp(3) : warning C4305: 'argument' : truncation from 'const double' to 'float'
D:\程序\vc6.0组\我的工作空间\第六题\第12题\main.cpp(3) : warning C4305: 'argument' : truncation from 'const double' to 'float'

main.obj - 0 error(s), 2 warning(s)
[/Quote]
liaojiededepan 2008-07-08
  • 打赏
  • 举报
回复
1楼兄弟;
我把hs.cpp放到mb.h里侯
main.cpp编译有警告
提示:Compiling...
main.cpp
D:\程序\vc6.0组\我的工作空间\第六题\第12题\main.cpp(3) : warning C4305: 'argument' : truncation from 'const double' to 'float'
D:\程序\vc6.0组\我的工作空间\第六题\第12题\main.cpp(3) : warning C4305: 'argument' : truncation from 'const double' to 'float'

main.obj - 0 error(s), 2 warning(s)
taodm 2008-07-08
  • 打赏
  • 举报
回复
google 模板分离编译模式
xkyx_cn 2008-07-08
  • 打赏
  • 举报
回复
晕,模板要把声明和定义放在一起,都放到头文件

vc6一直有这个问题

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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