C ++ Switch语句错误

时间:2010-03-20 03:09:47

标签: c++ arrays object switch-statement case

我正在使用游戏循环的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 stats\n"
                     << "'/shop' will let you visit the weapon shop\n"
                     << "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 S\nName: " 
                     << heroName 
                     << "\nLevel: "
                     << heroLevel
                     << "\nExp: "
                     << heroExp << "/" << expForLevel[heroLevel]
                     << "\nGold: "
                     << heroGold
                     << "\nHealth: "
                     << heroHealth
                     << "\nPower: "
                     << heroPower
                     << "\nDefense: "
                     << heroDefense
                     << "\nWeapon: "
                     << weaponNameList[heroWeapon]
                     << "\nArmor: "
                     << armorNameList[heroArmor]
                     << "\n\n";
                system("PAUSE");
                gamestate = 2;
                break;

            case 3:
                break;
            }
        }

        return 0;
    }

4 个答案:

答案 0 :(得分:10)

通过它的声音,你有:

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

case 3:

为了达到案例3,编译器会在somevar的初始化之后生成跳转。

要修复,请使用大括号创建围绕变量声明的块:

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

答案 1 :(得分:6)

在堆栈框架中包装声明......呃...... 本地范围 ......:)

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;

答案 2 :(得分:3)

请考虑以下事项:

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

如果x为1,该怎么办?然后我们跳过 over 初始化i并开始使用它。这就是您所获得的:case 3可以访问case 2中的变量,但如果您使用它们,则您已开始使用它们而不运行初始化。

常见的解决方案是引入范围:

switch (x)
{
    case 0:
    {
        int i = 0;
    }
    case 1:
    {
        i = 5; // not possible, no i in this scope
    }
}

答案 3 :(得分:3)

修改

现在我们看到更多代码,问题很明显,这个

        case 2:
            Weapon *wCurrent = weaponList[heroWeapon];
            Armor *aCurrent = armorList[heroArmor];

声明两个变量,所以除非你将案例2的主体包裹在{}

中,否则你不能在它之后放一个案例

以下原始答案


在一个案例中声明的变量范围是包含开关的大括号,除非您添加一组额外的大括号。所以这样的事情有效。

    switch(gamestate)
    {
        case 0:
            foo a;
            break;
    }

但这允许案例1跳过a的初始化但仍引用它,因此它会产生错误。

    switch(gamestate)
    {
        case 0:
            foo a;
            break;

        case 1:
            break;
    }

所以你需要这样做,现在a的范围仅限于案例0。

    switch(gamestate)
    {
        case 0:
            {
            foo a;
            }
            break;

        case 1:
            break;
    }

很明显,当您编辑代码以省去不相关的内容时,您还删除了导致问题的代码。 ;)