我有一个GameLobby
课程,其中列出了当前有效的游戏。我想从GameLobby
(单例)对象中获取活动游戏,并将其显示给用户。
(免责声明:我对C ++很陌生,所以下面的代码并不是很完美。也不是完整的代码,但我确信已经包含了所有相关的指令。)
首先是一些定义
class GamesMenu : public MyGameLayer
{
private:
std::vector<Game*>* _activeGames;
void displayGamesList();
void refreshGamesList();
};
和
class MyGameLayer : public cocos2d::CCLayer
{
private:
GameLobby* _gameLobby;
public:
GameLobby* getGameLobby();
};
和
GameLobby* MyGameLayer::getGameLobby()
{
return _gameLobby;
}
现在问题就在眼前。我想执行GamesMenu::refreshGamesList()
,如下所示:
void GamesMenu::refreshGamesList()
{
GameLobby* gameLobby = getGameLobby();
if (gameLobby) {
_activeGames = gameLobby->getActiveGames();
Game* game = _activeGames->at(0); // For debug purposes only - this game is NOT garbage
}
displayGamesList();
}
其中
std::vector<Game*>* GameLobby::getActiveGames()
{
if (_loggedInPlayer) {
refreshActiveGames(_loggedInPlayer->GetPlayerToken());
} else {
refreshActiveGames("");
}
return &_activeGames;
};
和std::vector<Game*> _activeGames
是GameLobby
的私人成员。
然而,当执行命中displayGamesList()
时,事情变得非常糟糕
void GamesMenu::displayGamesList()
{
for (unsigned i = 0; i < _activeGames->size(); i++) {
Game* game = _activeGames->at(i); // The contents of game is garbage. Why?
std::string opponentName = game->GetOpponentName(); // This I don't even want to talk about
};
/* Supressed drawing stuff */
}
当我在game
中检查GamesMenu::refreshGamesList
时,game
的内容似乎很好。当我在game
中检查GamesMenu::displayGamesList
时,内容都是垃圾。就好像向量的元素指向错误的数据或其他东西。
任何人都可以帮助我解决这个烂摊子的问题吗?谢谢! :)