C ++菜单代码 - 运行另一个类

时间:2017-03-31 11:43:25

标签: c++

好的,所以我接近完成这个简单的C ++游戏的工作,现在只需要将类连接起来就可以说,当某个事件发生时,它会开始从另一个类运行代码。 / p>

然而,我的菜单是一个特别顽固的问题。我试图设置它,所以当用户输入选项“1”作为他们的选择时,它开始在另一个C ++文件中运行地图代码,但它没有它。

每当我尝试运行代码时,它会给我以下3个错误。

1   IntelliSense: transfer of control bypasses initialization of:
        variable "A" (declared at line 37)  e:\C++ - Copy\Menu_3\Menu_3\Menu.cpp    27  3   Menu_3

2   IntelliSense: class "Map" has no member "standby"   e:\C++ - Copy\Menu_3\Menu_3\Menu.cpp    38  6   Menu_3

3   IntelliSense: return value type does not match the function type    e:\C++ - Copy\Menu_3\Menu_3\Menu.cpp    46  9   Menu_3

这是我的菜单代码:

#pragma once
#include "Map.h"
#include "Menu.h"
#include <iostream>
#include <string>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

Menu::Menu()
{
}

void menu()
{
    int choice;

    bool gameOn = true;
    while (gameOn != false){
        cout << " 1 - Play\n";
        cout << " 2 - Quit\n";

        cin >> choice;

        switch (choice)
        {
        case 1:
            cout << "Your adventure starts now!\n";
            system("PAUSE");

            cout << "Welcome to ATAG brave adventurer!\n";
            system("PAUSE");

            //Starts the next part of the game, in this case, the map
            Map A;
            A.standby();

        //Ends the game
        case 2:
            system("PAUSE");
            exit(0);
        }
    }
    return 0;
}

Menu::~Menu()
{
}

仅供参考,我正在使用Visual Studio 2013以防万一。 任何人都有任何建议,因为这可能是最后一件事,因为代码阻止我完成项目,如果有人能告诉我如何让这三个错误消失,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是一个很好的解释,为什么你有这个问题 - &gt; Why can't variables be declared in a switch statement?

如何修复 - 只需将case正文括在花括号中即可。这将变量A的范围限制为花括号(不是整个switch)。

case 1: {
    cout << "Your adventure starts now!\n";
    system("PAUSE");
    cout << "Welcome to ATAG brave adventurer!\n";
    system("PAUSE");
    Map A;
    A.standby();
} break;