所以我有一个这样声明的 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/