关于类模板问题?难到潭浩强的书又出错了?
跟书的代码一样,编译就是报错。。。。求解
#include<iostream>
using namespace std;
template<class numtype>
class Compare
{
public:
Compare(numtype a,numtype b)
{x=a;y=b;}
numtype max()
{
return(x>y)?x:y;
}
numtype min()
{return (x<y)?x:y;}
private:
numtype x,y;
};
int main()
{
Compare<int>cmp1{4,7};
cout<<cmp1.max()<<"is the Maximum of two integer numbers."<<endl;
cout<<cmp1.min()<<"is the minimum of two integer 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;
return 0;
}
[解决办法]
Compare<int>cmp1(4,7);
Compare<float>cmp2(45.78,93.6);
[解决办法]
LZ眼花了吧,呵呵,不是大括号
[解决办法]
Compare<int>cmp1{4,7};
不能用大括号