Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在向量循环c+中使用对象方法+;_C++_Vector - Fatal编程技术网

C++ 在向量循环c+中使用对象方法+;

C++ 在向量循环c+中使用对象方法+;,c++,vector,C++,Vector,这是我的班级: class A { public: int getVal(){return m_val;}; private: int m_val = 1; } class B { public: void print(); private: std::vector<A*> m_vA; } A类 { 公众: int getVal(){return m_val;}; 私人: int m_val=1; } B类 { 公众: 作废打印(); 私人: st

这是我的班级:

class A
{
public:
    int getVal(){return m_val;};
private:
    int m_val = 1;
}

class B
{
public:
    void print();
private:
    std::vector<A*> m_vA;
}
A类
{
公众:
int getVal(){return m_val;};
私人:
int m_val=1;
}
B类
{
公众:
作废打印();
私人:
std::向量m_vA;
}
现在我已经出版了

void B::print()
{
    std::vector<A>::iterator it;
for(auto it = m_vA.begin(); it != m_vA.end(); it++) {
    cout << *it.getVal() << endl;
}
}
void B::print()
{
std::vector::it迭代器;
for(auto it=m_vA.begin();it!=m_vA.end();it++){

cout有各种各样的问题。首先,您有一个运算符优先级问题。有问题的表达式如下所示:

*(it.getVal())
你可以用任何一种

it->getVal()

接下来,向量的类型为
std::vector
,但迭代器的类型为
std::vector::iterator
。这无法工作。我的答案取决于向量的类型为
std::vector
。如果是
std::vector
,则需要额外的反引用级别:

(*it)->getVal()

有各种各样的问题。首先,您有一个运算符优先级问题。有问题的表达式如下所示:

*(it.getVal())
你可以用任何一种

it->getVal()

接下来,向量的类型为
std::vector
,但迭代器的类型为
std::vector::iterator
。这无法工作。我的答案取决于向量的类型为
std::vector
。如果是
std::vector
,则需要额外的反引用级别:

(*it)->getVal()
(*it).getVal()
it->getVal()
(*it).getVal()
it->getVal()
。谢谢,解决方案是(*it)->getVal()。谢谢,解决方案是(*it)->getVal()