在c++中从一个函数传递到另一个函数

Passing arguments from function to function in c++

本文关键字:函数 一个 另一个 c++      更新时间:2023-10-16

我是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 belownn"
         << "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,只是忘记将其作为最后一个参数传递:-)

这是因为您将mMenu定义为接受四个参数,但只使用三个

mMenu (choice, sure, quit);

什么是错误的是mMenu不接受3个参数。

有两种方法可以完成此编译:

  • 把mMenu改成3个参数
  • 使用当前的4个参数调用mMenu。
相关文章: