1

私のエラー:

|21|error: 'main' was not declared in this scope|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

私の実際のプログラムは根本的に異なり、はるかに大きくなっています (したがって、不要なヘッダーがあります)。私は自分の問題をすばやく実証するためにこれを書きました。その void 関数の範囲から出て、メイン (プログラムのメニューが実際にある場所) に戻るにはどうすればよいですか?

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

1 に答える 1

2

ただ戻ってください:

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

void 関数から戻ることができます。オブジェクトを返すことはできません。

さらなる注意として、あなたは決して電話してはいけませんmain。main の下に置きcontinueChoice、それを forward 宣言することでこれをコンパイルできますが、そうすると無限再帰ループが作成され、すべてにとって非常に悪いことになります。

于 2014-12-24T00:06:12.600 に答える