-1

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

vector <Game *> games;

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

public:

    // Rest of .h

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

我正在尝试games使用迭代器迭代我的向量,并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’

知道发生了什么/如何获得所需的功能吗?

4

2 回答 2

2

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

*it_games->hello();

应该是这样的:

(*it_games)->hello();
于 2013-09-16T00:25:14.533 回答
2

->具有优先权,因此在 hello() 之前不会取消引用 it_games 指针。

改变

*it_games->hello();

(*it_games)->hello();
于 2013-09-16T00:27:07.210 回答