C ++切换语句案例错误

我正在使用游戏循环的switch语句编写一个简单的基于文本的RPG。 该程序工作正常,直到我试图添加另一个case语句,在这一点上它给了我以下三个错误:“跳转到案例标签”(错误发生在新增案例的行),和两个“十字架初始化” ClassName * objectName'“(在情况2中创建新对象时发生错误)。 我会粘贴重要的代码,如果有人需要更多,请告诉我。

int main(void)
{
    // add weapons to array
    Weapon *weaponList[12];
    // Rusty Sword
    weaponList[0] = new Weapon(0,0,0);
    weaponList[0]->SetAll(0,2,3);
    // Bronze Sword
    weaponList[1] = new Weapon(0,0,0);
    weaponList[1]->SetAll(1,5,10);
    // Bronze Battle Axe
    weaponList[2] = new Weapon(0,0,0);
    weaponList[2]->SetAll(2,15,30);
    // Iron Sword
    weaponList[3] = new Weapon(0,0,0);
    weaponList[3]->SetAll(3,25,70);

    // add armor to array
    Armor *armorList[12];
    // Worn Platemail
    armorList[0] = new Armor(0,0,0);
    armorList[0]->SetAll(0,2,3);
    // Bronze Chainmail
    armorList[1] = new Armor(0,0,0);
    armorList[1]->SetAll(1,5,8);
    // Bronze Platemail
    armorList[2] = new Armor(0,0,0);
    armorList[2]->SetAll(2,7,20);
    // Iron Chainmail
    armorList[3] = new Armor(0,0,0);
    armorList[3]->SetAll(3,15,60);

        while(gamestate != 8)
        {
            switch(gamestate)
            {
                case 0:
                cout << " /|    Welcome!n"
                     << " ||    n"
                     << " ||    n"
                     << " ||    n"
                     << "_||_   n"
                     << " 88    n"
                     << " 88    Name: ";
                cin  >> heroName;
                gamestate = GAME_STATE_MENU;
                break;

            case 1:
                cout << "n"
                     << "'/stats' will show you your statsn"
                     << "'/shop' will let you visit the weapon shopn"
                     << "secret commands: /setweapon #   /setarmor #   /setheroexp #n"
                     << "n";

                cout << "Command: ";
                cin  >> command;

                if (strcmp(command, "/stats") == 0)
                {
                    gamestate = 2;
                    break;
                }

                else if (strcmp(command, "/shop") == 0)
                {
                    gamestate = 3;
                    break;
                }

                else if (strcmp(command, "/fight") == 0)
                {
                    gamestate = 4;
                    break;
                }

                else if (strcmp(command, "/setweapon") == 0)
                {
                    cin >> testNum;
                    heroWeapon = testNum;
                    break;
                }

                else if (strcmp(command, "/setarmor") == 0)
                {
                    cin >> testNum;
                    heroArmor = testNum;
                    break;
                }

                else if (strcmp(command, "/setheroexp") == 0)
                {
                    cin >> testNum;
                    heroExp = testNum;
                    LevelUp();
                    break;
                }

                else if (strcmp(command, "/exit") == 0)
                {
                    gamestate = 8;
                    break;
                }

                else
                {
                    cout << "Please enter a valid command.n";
                    gamestate = 2;
                    break;
                }

            case 2:
                Weapon *wCurrent = weaponList[heroWeapon];
                Armor *aCurrent = armorList[heroArmor];
                heroWeaponPower = wCurrent->GetWeaponAttack();
                heroArmorDefense = aCurrent->GetArmorDefense();
                heroPowerDefault = ((heroLevel - 1) * 10) + 10;
                heroPower = heroPowerDefault + (heroStrength * 2) + heroWeaponPower;
                heroDefenseDefault = ((heroLevel - 1) * 2) + 5;
                heroDefense = heroDefenseDefault + (heroAgility / 5) + heroArmorDefense;
                heroHealthDefault = (heroLevel * 5) + 20;
                heroHealth = heroHealthDefault + (heroStamina * 10);
                cout << "nS T A T SnName: " 
                     << heroName 
                     << "nLevel: "
                     << heroLevel
                     << "nExp: "
                     << heroExp << "/" << expForLevel[heroLevel]
                     << "nGold: "
                     << heroGold
                     << "nHealth: "
                     << heroHealth
                     << "nPower: "
                     << heroPower
                     << "nDefense: "
                     << heroDefense
                     << "nWeapon: "
                     << weaponNameList[heroWeapon]
                     << "nArmor: "
                     << armorNameList[heroArmor]
                     << "nn";
                system("PAUSE");
                gamestate = 2;
                break;

            case 3:
                break;
            }
        }

        return 0;
    }

通过它的声音,你有:

case 2:
    Type somevar = ...;
    ...
    break;

case 3:

为了达到somevar情况,编译器生成一个跳过somevar初始化的somevar

为了解决这个问题,使用大括号来创建围绕变量声明的块:

case 2:
    {
    Type somevar = ...;
    ...
    }
    break;

将声明封装在堆栈中...呃... 本地范围 ... :)

switch(gamestate)
{
    case 0:
    {
      Apple a;
      a.DoSomething();
    }
    break;

    case 1: /* etc. */ break;
    case 2: /* etc. */ break;
}

...或将它们移动到开关外部:

Apple A;
switch(gamestate)
{
     case 0: a.DoSomething(); break;

考虑以下:

switch (x)
{
    case 0:
        int i = 0;
    case 1:
        i = 5;
}

如果x是1呢? 然后我们跳过i的初始化并开始使用它。 这就是你所得到的: case 3可以访问case 2变量,但是如果你使用它们,你已经开始使用它们而不需要运行它们的初始化。

常见的解决办法是引入范围:

switch (x)
{
    case 0:
    {
        int i = 0;
    }
    case 1:
    {
        i = 5; // not possible, no i in this scope
    }
}
链接地址: http://www.djcxy.com/p/84431.html

上一篇: C++ Switch Statement Case Error

下一篇: How to Initialize a Point Array?