c++ - 调用对象 vector 的成员函数

标签 c++ vector iterator

所以我有一个这样声明的 vector :

vector <Game *> games;

我在 Game 类中有一个函数,如下所示:

public:

    // Rest of .h

    void hello(){
        cout << "hello" << endl;
    }

我正在尝试使用迭代器迭代我的 games vector ,并每次调用 hello() 函数:

vector <Game *>::const_iterator it_games = games.begin();

for (it_games = games.begin(); it_games != games.end(); it_games++) {
    *it_games->hello();
}

但是当我尝试编译时,我不断收到这个错误:

main.cpp: In function ‘void summary(std::vector<Game*, std::allocator<Game*> >, std::string, std::string, std::string, std::string)’:
main.cpp:56: error: request for member ‘hello’ in ‘* it_games. __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = Game* const*, _Container = std::vector<Game*, std::allocator<Game*> >]()’, which is of non-class type ‘Game* const’

知道发生了什么/如何获得我想要的功能吗?

最佳答案

operator* 的优先级低于 operator->。所以这个:

*it_games->hello();

应该是这样的:

(*it_games)->hello();

关于c++ - 调用对象 vector 的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18818842/

相关文章:

python - 如何使用异步 for 循环遍历列表?

c++ - 函数中不调用复制赋值

c++ - 带有 Qt 的 GUI 的 "About"消息框

c++ - 计算平方根时出现SegFault错误(牛顿法)

r - 选择命名向量中的元素

java - 在 Object[] 数组上实现 Java 迭代器和可迭代接口(interface)

c++ - Base64编码失败

c++ - 如何正确使用带有 boost const_buffers vector 的 boost async_write?

c - 转到特定元素 C

rust - 使用函数样式(flat_map等)实现递归迭代器的麻烦,该函数样式可处理错误并产生Result <…>类型的项