[英]How do I get out of my current scope from void so I can get back to main? What do I need to do?
My error: 我的错误:
|21|error: 'main' was not declared in this scope|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
My actual program is radically different and much larger (hence the unneeded headers). 我的实际程序完全不同,并且更大(因此不需要了标头)。 I just wrote this to demonstrate my issue quickly.
我只是写这篇文章来快速演示我的问题。 How do I get out of the scope of that void function and get back to main (where the menu of my program actually lies)?
我如何脱离该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
}
Just return: 只需返回:
void somefunc() {
if (something)
if (something_else)
return;
}
}
}
You can return from a void function. 您可以从void函数返回。 You just cant return an object.
您只是无法返回对象。
As a further note, you should never be calling main
. 进一步说明,您永远不应调用
main
。 You could make this compile by putting continueChoice
under main and forward declaring it, but you would then be creating an infinite recursive loop which is very bad for everything. 您可以通过将
continueChoice
放在main并向前声明它来进行编译,但是随后您将创建一个无限递归循环,这对所有事情都非常不利。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.