如何在c ++中创建类的向量 [英] how to make vector of class in c++
本文介绍了如何在c ++中创建类的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在定义类的向量时遇到问题。
class classtest {
public :
classtest(){x = y = 2 ;}
int getx(){ return x;}
< span class =code-keyword> int gety(){ return y;}
void setx( int interData){x = interData;}
void sety ( int interData){y = interData;}
private :
int x;
int y;
};
vector< classtest> test2( 10 ,classtest());
for (i = 0 ; i< test2.size(); i ++)
{
cout<<(test2.at(i)) - > getx(); // 编译器在此行中返回错误
}
解决方案
您应该非常仔细地阅读编译器错误并据此作出回应。
试试这个
cout<<(test2.at(i ))。的getX()
i have a problem when define a vector of class .
class classtest{
public:
classtest(){x=y=2;}
int getx(){return x;}
int gety(){return y;}
void setx(int interData){x=interData;}
void sety(int interData){y=interData;}
private:
int x;
int y;
};
vector <classtest> test2(10,classtest());
for(i=0;i<test2.size();i++)
{
cout<<(test2.at(i))->getx();// compiler return an error in this line
}
解决方案
You should read the compiler error very carefully and respond accordingly.
Try this
cout<<(test2.at(i)).getx()
这篇关于如何在c ++中创建类的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文