简体   繁体   中英

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)?

#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. You just cant return an object.

As a further note, you should never be calling 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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