簡體   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