0

我有一个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 */
}

当我检查gameGamesMenu::refreshGamesList, 的内容game似乎很好。当我检查gameGamesMenu::displayGamesList,内容都是垃圾。就好像向量的元素指向错误的数据或什么的。

谁能帮我解开这个烂摊子?谢谢!:)

4

0 回答 0