【问题标题】:accessing a method using a vector iterator使用向量迭代器访问方法
【发布时间】:2015-06-16 20:00:36
【问题描述】:

我有两个 C++ 对象。其中第一个包含第二个声明的向量:

std::vector<WormCell> cells;    // The dynamic array of the worms cells

在第一个方法中,我尝试使用迭代器遍历向量并在第二个方法中调用方法,但出现错误。调用该方法的代码是:

void Worm::drawWorm(sf::RenderWindow &window)
{
    for (std::vector<WormCell>::iterator it = cells.begin() ; it != cells.end(); ++it)
    {
        sf::Vector2f pos = it->getPosition;
        circle.setPosition(pos);
    }
}

错误是:

错误:绑定到函数的指针只能用于调用函数。

发生在 it->getPosition 上。

如何使用迭代器访问单元格中的方法?

【问题讨论】:

  • C++ 中的call 称为call,因为它包含函数call 运算符()it-&gt;getPosition 行中的函数调用运算符在哪里???如果那里没有函数调用运算符,那你为什么称它为 call 呢?

标签: c++ methods vector


【解决方案1】:

调用函数需要参数列表:

it->getPosition();
               ^^

【讨论】:

    【解决方案2】:

    您正在调用该函数,就好像它是一个数据成员一样。

    不要忘记括号。试试:

    it->getPosition() 
    

    而不是

    it->getPosition
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多