C++类的模板问题

#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;

倒数的第二句和第三句中函数调用中少了()。你再修改一下!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-06-17

没有问题啊:

#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;

}

相似回答