使用向量迭代器访问方法

accessing a method using a vector iterator

我有两个 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.

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

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

不要忘记括号。尝试:

it->getPosition() 

而不是

it->getPosition

希望对您有所帮助!

调用函数需要参数列表:

it->getPosition();
               ^^