C++ 菜单代码 - 运行另一个类
C++ Menu Code - Running another Class
好的,所以我即将完成我在这个简单C++游戏上的工作,现在只需将类连接在一起,可以这么说,这样当某个事件发生时,它将开始从另一个类运行代码。
然而,我的菜单是一个特别顽固的问题。我试图设置它,以便当用户输入选项"1"作为他们的选择时,它开始在另一个C++文件中运行地图代码,但它没有。
每当我尝试运行代码时,它都会给我以下 3 个错误。
1 IntelliSense: transfer of control bypasses initialization of:
variable "A" (declared at line 37) e:C++ - CopyMenu_3Menu_3Menu.cpp 27 3 Menu_3
2 IntelliSense: class "Map" has no member "standby" e:C++ - CopyMenu_3Menu_3Menu.cpp 38 6 Menu_3
3 IntelliSense: return value type does not match the function type e:C++ - CopyMenu_3Menu_3Menu.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 - Playn";
cout << " 2 - Quitn";
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,以防有帮助。任何人都得到了任何建议,因为就代码而言,这可能是阻止我完成项目的最后一件事,如果有人能告诉我如何使这 3 个错误消失,我将不胜感激。
这是一个很好的解释,为什么你会遇到这个问题 -> 为什么不能在 switch 语句中声明变量?
如何修复 - 只需将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;
相关文章:
- 运行同一解决方案的另一个项目的项目
- C++线程:如何在一个线程仍在运行时阻止另一个线程执行 (Win32)
- 使用另一个程序调用正在运行的 C++ 程序中的函数
- System Verilog DPI - 在 cpp 中运行并行线程,在 SV 中运行另一个并行线程
- 使用 (cin) 用户输入将其粘贴到 std::system 中,并在另一个终端中运行带有输入的命令
- 如何在 qt 中从另一个线程运行 qt并发时关闭程序
- 助推.如何在另一个测试单元中运行一个测试组件
- 给定的两个代码有什么区别.一个在 ideone 上运行时超出时间限制,另一个工作正常
- C++ 减慢另一个正在运行的进程
- 在另一个 QThread 上运行成员方法时,无法将事件发送到其他线程拥有的对象
- 字符* 复制到另一个字符* 运行时错误
- C++ 菜单代码 - 运行另一个类
- 我无法使用 c++(代码块)中的 fstream 将文件内容复制到另一个。如何运行该文件?
- 运行EXE形成另一个EXE并传递参数
- 只有当输入文件确实包含要读取的另一个值时,我才能让getline(variable,n)运行
- 使用 memcpy 将动态数组的内容复制到另一个动态数组上会产生运行时错误
- 将信息从运行函数发送到另一个
- 我试图运行将文本从一个文件复制到另一个文件的程序
- 一个应用程序可以运行另一个应用软件的代码吗
- UNIX中两个进程之间通信的最佳方式:一个运行ruby服务器,另一个运行C++客户端