我是 C++ 的新手,我正在尝试制作国际象棋游戏。
这是我尝试制作主菜单的东西,但遇到了麻烦。这是我的代码片段:
int mMenu(int&, char&, bool&, char&);
int main(char&)
{
int choice;
char sure;
bool quit = false;
char ctrl // used for the control from main menu to main()
mMenu (choice, sure, quit);
do
{
if (ctrl == a)
NewGame();
else if (ctrl == b)
LoadGame();
else
quit = true;
}
while (quit == true);
return 0;
}
int mMenu(int& choice, char& sure, bool& quit, char& ctrl,)
{
do
{
cout << " Chess "
<< "------------------------ Main Menu ------------------------\n"
<< "Please choose what operation you'd like to perform from the menu below\n\n"
<< "1. New Game.\n"
<< "2. Load Game.\n"
<< "Exit.\n"
<< "Your choice: ";
cin >> choice;
if (choice == 1)
{
cout << "Are you sure you wish to start a new game? (Y/N) ";
cin >> sure;
if (sure != 'Y')
clrscr();
else
{
ctrl = a;
quit = true;
}
else if (choice == 2)
{
ctrl = b;
quit = true;
}
else if (choice == 3)
{
cout << "Are you sure you wish to exit? (Y/N) ";
cin >> sure;
if (sure != 'Y')
clrscr();
else
{
quit = true;
ctrl = c;
}
}
}
}
while (quit = true);
return ctrl;
}
根据该代码,我的编译器 (visual c++) 说 int main() 函数,mMenu 不带 3 个参数。哪里出了问题,我该如何让它发挥作用?
提前致谢。
另外,正如您所看到的,我正在尝试使用 clrscr();但是编译器标记它说它找不到它的定义,尽管在 #include 中加入了任何想法?
最佳答案
它真的不需要3个参数,它需要4个:
int mMenu(int& choice, char& sure, bool& quit, char& ctrl,)
// << the "," in the end
// shouldn't be there
如何解决?添加缺少的参数:
mMenu (choice, sure, quit, ctrl/*<a ctrl parameter goes here>*/);
你甚至定义了变量ctrl
,只是忘了把它作为最后一个参数传递:-)
关于c++ - 在 C++ 中将参数从一个函数传递到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8372208/