繁体   English   中英

我如何从无效中脱离当前范围,以便回到主要领域? 我需要做什么?

[英]How do I get out of my current scope from void so I can get back to main? What do I need to do?

我的错误:

|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
}

只需返回:

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

您可以从void函数返回。 您只是无法返回对象。

进一步说明,您永远不应调用main 您可以通过将continueChoice放在main并向前声明它来进行编译,但是随后您将创建一个无限递归循环,这对所有事情都非常不利。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM