#include<iostream>
#include<string.h>
using namespace std;
template <class T>
class anytype
{ T x,y;
public :
anytype (T a,T b):x(a) ,y(b){}
T getx(){return x;}
T gety(){return y;}
};
int main()
{anytype <int> i (3,2);
anytype <char> d('d','v');
cout<<"x="<<i.getx<<endl<<"y="<<i.gety<<endl;
cout<<"x="<<d.getx<<endl<<"y="<<d.gety<<endl;
}
为什么结果是这个呢
cout<<"x="<<i.getx()<<endl<<"y="<<i.gety()<<endl;
cout<<"x="<<d.getx()<<endl<<"y="<<d.gety()<<endl;
倒数的第二句和第三句中函数调用中少了()。你再修改一下!
没有问题啊:
#include<iostream>
#include<string.h>
using namespace std;
template <class T>
class anytype
{ T x,y;
public :
anytype (T a,T b):x(a) ,y(b){}
T getx(){return x;}
T gety(){return y;}
};
int main()
{anytype <int> i (3,2);
anytype <char> d('d','v');
cout<<"x="<<i.getx()<<endl<<"y="<<i.gety()<<endl;
cout<<"x="<<d.getx()<<endl<<"y="<<d.gety()<<endl;
}