1

我的错误:

|21|error: 'main' was not declared in this scope|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

我的实际程序完全不同并且更大(因此不需要标题)。我写这篇文章是为了快速展示我的问题。我如何摆脱该 void 函数的范围并返回 main (我的程序菜单实际所在的位置)?

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <limits>

int continueint;

using namespace std;

class handles {
public:
void continueChoice()
{
    cout<<"[1] Go back to the main menu"<<endl;
    cout<<"[2] Terminate the program"<<endl;
    cin>>continueint;
    switch (continueint)
    {
    case 1:
        main();   // This is where my problem lies
    case 2:
        break;
    }
}
}handlers;

int main(int nNumberofArgs, char*pszArgs[])
{
cout<<"A message here."<<endl;
system("PAUSE");
handlers.continueChoice(); //Calls the void function in the above class
}
4

1 回答 1

2

只需返回:

void somefunc() {
    if (something)
        if (something_else)
            return;
        }
    }
}

您可以从 void 函数返回。你只是不能返回一个对象。

进一步说明,您永远不应该调用main. 您可以通过continueChoice在 main 和 forward 声明它来进行编译,但是您将创建一个无限递归循环,这对一切都非常不利。

于 2014-12-24T00:06:12.600 回答