C ++类枚举成员变量

时间:2012-02-05 19:20:29

标签: c++ class enumeration member-functions

我有一个带有枚举类型GameStates的类。在(公共)构造函数中,我初始化GameStates,如下所示:

GameStates enumGameState = Ready;

然后在公共方法run()中我有一个这样的开关:

switch(enumGameState)
        {
        case Ready:
            if (theGameEngine->KeyHit(Key_Space))
            {
                enumGameState = Firing;
                cout << "\nGame State moved to Firing";
            }   // End if
            break;
        case Firing:
            if (theGameEngine->KeyHit(Key_Space))
            {
                enumGameState = Contact;
                cout << "\nGame State moved to Contact";
            }   // End if
            break;
        case Contact:
            if (theGameEngine->KeyHit(Key_Space))
            {
                enumGameState = Over;
                cout << "\nGame State moved to Over";
            }   // End if
            break;
        case Over:
            break;
        };  // End of GameState switch

虽然代码没有错误,但没有一个状态得到满足。我应该如何访问enumGameState的值?

编辑:所有课程代码。

class Game
{
private:
    Block* arrBlocks[10][10];   
    //IMesh* objBlockMesh;
    IMesh* objGunMesh;
    IMesh* objDummyMesh;
    Gun* objGun;
    int Game::intSpeed;
    I3DEngine* theGameEngine;
    float fltSkyboxXCo;
    float fltSkyboxYCo;
    float fltSkyboxZCo;
    float fltFloorXCo;
    float fltFloorYCo;
    float fltFloorZCo;

    enum GameStates{Ready,Firing, Contact, Over};
    GameStates enumGameState;

public: 

    Game(I3DEngine* the3dengine)
    {
        Game::theGameEngine = the3dengine;
        theGameEngine->StartWindowed();

        // Add default folder for meshes and other media
        theGameEngine->AddMediaFolder( "C:\\TL-Engine\\Media\\AssigmentTwo\\Media" );

        //intSpeed = 1;

        Game::DrawBasicScene(theGameEngine);
        Game::DrawBlocks();
        Game::CreateAGun();
        Bullet::Bullet(theGameEngine);
        Game::enumGameState = Ready;
    }   // End of Constructor


private:    


    void DrawBlocks()
    {
        float fltBlockOffSet = 12.0f;
        float fltLeftMost = -54.0f;
        float fltBlockZCo = 120.0f;
        float fltBlockYCo = 5.0f;
        float fltCurrentXCo;
        float fltCurrentYCo = 5.0f;
        float fltCurrentZCo = 120.0f;
        // Stick 10 blocks in an array
        // Display them

        for(int i = 0; i < 10; i++)
        {
            if (i == 1) // Once i have created the first row all the other blocks are going to be created in a hidden state
            {
                fltCurrentYCo = -50.0f;
            }
            for(int j = 0; j < 10; j++)
            {
                fltCurrentXCo = ((float)j*fltBlockOffSet) + fltLeftMost;    // Cast j into a float explicitly so that it doesn't it implicitly
                arrBlocks[i][j] = new Block(theGameEngine, fltCurrentXCo, fltCurrentYCo, fltCurrentZCo);
                if(fltCurrentYCo < 0)
                {
                    arrBlocks[i][j]->SetBlockState(Block::Destroyed);
                }   // End if
                else
                {
                    arrBlocks[i][j]->SetBlockState(Block::New);
                }
            }   // End of inner loop

            fltCurrentZCo += fltBlockOffSet;

        }   // End of outer loop

    }

    void CreateAGun()
    {
        // Create a gun
        Gun::Gun(theGameEngine);
    }

public:
    void Game::Run()
    {
        //Start watching input in a while loop
        // The main game loop, repeat until engine is stopped
        while (theGameEngine->IsRunning())
        {
            // Draw the scene
            theGameEngine->DrawScene();
            if (theGameEngine->KeyHit(Key_Escape))
            {
                theGameEngine->Stop();
            }

            if)theGameEngine->KeyHit(Key_Space))
            {
                cout << "\n space";
            }

            GameStates currentGameState = enumGameState;
            switch(enumGameState)
            {
            case Ready:
                if (theGameEngine->KeyHit(Key_Space))
                {
                    enumGameState = Firing;
                    cout << "\nGame State moved to Firing" << endl;
                }   // End if
                break;
            case Firing:
                if (theGameEngine->KeyHit(Key_Space))
                {
                    enumGameState = Contact;
                    cout << "\nGame State moved to Contact" << endl;
                }   // End if
                break;
            case Contact:
                if (theGameEngine->KeyHit(Key_Space))
                {
                    enumGameState = Over;
                    cout << "\nGame State moved to Over" << endl;
                }   // End if
                break;
            case Over:
                break;
            };  // End of GameState switch
        }

    }
};  // End of Game Class

4 个答案:

答案 0 :(得分:4)

如果您的构造函数具有以下代码行,则逐字:

GameStates enumGameState = Ready;

然后您刚刚完成的是在构造函数方法中创建 local 变量enumGameState并对其进行初始化。一旦构造函数完成,它就会超出范围,并且它的值会丢失。

据推测,您还有一个成员变量enumGameState,其值未初始化,因此您的switch语句将使用虚假值运行。

创建与成员变量同名的局部变量是C ++中 shadowing 的一个示例,它通常表示错误。因此,如果隐藏变量,某些编译器(如GCC)可能会显示警告;有关详细信息,请参阅this answer

答案 1 :(得分:1)

您必须定义此枚举才能使用它:

enum GameState
{
    Ready,
    Firing,
    Contact,
    Over
};

然后课程Game可能如下所示:

class Game
{
public:
    Game(GameState gs = Ready) : gs(gs) { }
    void update()
    {
        switch (gs)
        {
        case Ready: cout << "Ready\n"; gs = Firing; break;
        case Firing: cout << "Firing\n"; gs = Contact; break;
        case Contact: cout << "Contact\n"; gs = Over; break;
        case Over: cout << "Over\n"; break;
        default: break;
        }
    }
private:
    GameState gs;
};

这是main

int main()
{
    Game g;
    g.update();
    g.update();
    g.update();
    g.update();
    return 0;
}

输出:

Ready
Firing
Contact
Over

答案 2 :(得分:1)

我根本不相信这个类本身有任何问题,我发现当我运行游戏时,main()从未输入,因此该类永远不存在。它让我处于另一个困境中,因为那是绘制游戏场景的地方,看起来似乎没有问题。

谢谢你帮我调试游戏类代码的时间。

答案 3 :(得分:1)

感谢您的帮助。问题不在于班级或班级的代码oustide。这是视觉工作室的一个问题,我不确定是什么。当代码被复制并添加到一个新项目中时,它编译得很完美,并且所有在main中的断点都被击中了。

故事的道德:首先尝试一下。