| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 479 人关注过本帖
标题:这个程序为什么会出错 ?
只看楼主 加入收藏
nala
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-9-2
收藏
 问题点数:0 回复次数:3 
这个程序为什么会出错 ?
#include <iostream>
using namespace std;
template<class numtype>
class Compare
{
public:
Compare(numtype xx,numtype yy):x(xx),y(yy){}
numtype max();
numtype min();
private:
numtype x,y;
};
numtype Compare::max()
{
return ((x>y)?x:y);
}
numtype Compare::min()
{
return ((x>y)?y:x);
}
void main()
{
Compare<float> ci(1.22,2,4);
cout << ci.max() << endl;
cout << ci.min() << endl;
}
搜索更多相关主题的帖子: private include public 
2006-09-04 20:10
ww84020209
Rank: 1
等 级:新手上路
帖 子:190
专家分:0
注 册:2006-8-21
收藏
得分:0 
以下是引用nala在2006-9-4 20:10:52的发言:
#include <iostream>
using namespace std;
template<class numtype>
class Compare
{
public:
Compare(numtype xx,numtype yy):x(xx),y(yy){}
numtype max();
numtype min();
private:
numtype x,y;
};
template<class numtype>
numtype Compare<numtype>::max()
{
return ((x>y)?x:y);
}
template<class numtype>
numtype Compare<numtype>::min()
{
return ((x>y)?y:x);
}
void main()
{
Compare<float> ci(1.22,2.4);
cout << ci.max() << endl;
cout << ci.min() << endl;
}


2006-09-04 20:44
gengshen126
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-9-3
收藏
得分:0 
修改为:
#include <iostream>
using namespace std;
template<class numtype>
class Compare
{
public:
Compare(numtype xx,numtype yy):x(xx),y(yy){}
numtype max();
numtype min();
private:
numtype x,y;
};
template<class numtype> numtype Compare<numtype>::max()
{
return ((x>y)?x:y);
}
template<class numtype> numtype Compare<numtype>::min()
{
return ((x>y)?y:x);
}
void main()
{
Compare<float> ci(1.22,2.4);
cout << ci.max() << endl;
cout << ci.min() << endl;
}
模版类的成员函数必须是模版函数,将Max() Min()正确定义为模版函数就行了。
By the way,2.4写成2,4了。

No problem but many questions on my furture way.
2006-09-04 20:57
nala
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-9-2
收藏
得分:0 

呵呵,精辟,在此谢过了

2006-09-06 08:43
快速回复:这个程序为什么会出错 ?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013989 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved