|
发表于 2009-11-3 23:40:20
|
显示全部楼层
IP:江苏扬州
修改为:
#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了。 |
|