简体   繁体   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?

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.

相关问题 如何获得此void *的值? - How do I get the value of this void* back? 如何从我的qt项目中获取脚本(python)的路径,以便可以在执行过程中运行它? - How do I get the path of a script(python) from my qt project, so I can run it during execution? 如何获取当前流程的作业对象(如果有)? - How do I get the job object (if any) for my current process? 将 unique_ptr 转换为 void* 并返回,我需要释放吗? - Convert unique_ptr to void* and back, do I need to free? 如何使我的构造函数和函数起作用,以便我的main()能够同时显示字符串和int数据? - How do I get my constructor and functions to work so my main() is able to display both the string and int data? 我需要做什么才能让 cocoapods 与 C++ 一起构建? - What do I need to do to get cocoapods to build with C++? 所以我不能dynamic_cast &lt;functionPointer&gt;((((void *)(myFuncPtr))))? 我该怎么办? - So I can't dynamic_cast< functionPointer >( ((void*)(myFuncPtr)) )? What should I do? 如何获得可能&lt;&gt; monad的值OUT? - How do I get the value OUT of my maybe<> monad? 如果 `size()` 为 0,我如何从`pop_back()` 得到错误? - How do I get an error from `pop_back()` if `size()` is 0? 如何将值从私有类返回给主类 - How do I return the value from the private class back to the main
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM