为什么我的矢量元素都是垃圾?

时间:2013-08-18 08:16:38

标签: c++ stdvector garbage

我有一个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*> _activeGamesGameLobby的私人成员。

然而,当执行命中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时,内容都是垃圾。就好像向量的元素指向错误的数据或其他东西。

任何人都可以帮助我解决这个烂摊子的问题吗?谢谢! :)

0 个答案:

没有答案