c++ - 在 C++ 中将参数从一个函数传递到另一个函数

标签 c++

我是 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/

相关文章:

c++ - 预编译头如何减少编译时间

未捕获 C++ 异常(Qt 项目)

c++ - 注入(inject) DLL 时,Explorer.exe 在启动时死锁

c++ - 从 main 返回时出现段错误(非常简短的代码,没有数组或指针)

c++ - -读取字符串字符时出错

c++ - 单独的 map 声明和初始化

c++ - 仅使用几乎相等的标准(无排序)从容器中删除重复项的最有效方法是什么

c++ - MFC CWnd 高度和宽度

c++ - boost filesystem - 如何在不解析的情况下获取符号链接(symbolic link)的最后写入时间?

c++ - 在 OpenCV C++ 中打印出(Mat)矩阵的值