C++ Switch 语句大小写错误

标签 c++ arrays object switch-statement case

我正在使用游戏循环的 switch 语句编写一个简单的基于文本的 RPG。该程序工作正常,直到我尝试添加另一个 case 语句,此时它给了我以下三个错误:“跳转到 case 标签”(错误发生在新添加的 case 行),以及两个“crosses initialization of ' 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;
    }

最佳答案

根据它的声音,你有:

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

case 3:

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

要修复,请使用大括号在变量声明周围创建一个 block :

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

关于C++ Switch 语句大小写错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2481749/

相关文章:

c++ - 如何使用宏替换赋值操作以用于锁定函数

c++ - 在 C++ 中以 'D' 作为指数前缀读取双科学记数法

java 。如何从两个列表中删除重复的对象

c++ - C++中类对象的创建

c - 将数组中的随机字符串提取到 C 中的 printf 中

javascript - 无法访问对象值

c++ - 使用 Boost::odeint 和 Eigen::Matrix 作为状态 vector

c++ - Windows 命令行历史记录

c - for 循环查找数组中的最小元素

python - 查找不同维度数组的最近邻居