在c ++中将参数从函数传递给函数

时间:2011-12-04 00:21:20

标签: 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 ++)说在main()函数中,mMenu不带3个参数。有什么问题,如何让它发挥作用?

提前致谢。

另外你可以看到我正在尝试使用clrscr();但编译器正在标记它说无法找到它的定义,尽管在#include中添加了任何想法?

3 个答案:

答案 0 :(得分:2)

它实际上不需要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,只是忘了将它作为最后一个参数传递: - )

答案 1 :(得分:0)

这是因为你已经将mMenu定义为接受四个参数,但只用三个

来调用它
mMenu (choice, sure, quit);

答案 2 :(得分:0)

什么是错误的,mMenu没有3个参数。需要4个。

有两种方法可以进行编译:

  • 更改mMenu以便它需要3个参数。
  • 使用当前的4个参数调用mMenu。