By defining a normal main
that only contains a call to your other function. Like this:
int main(int, char**) {
return Main().main();
}
Pergunta
Simple question, how can I run a program having main inside a class? I have a code:
MojSwiat2.cpp:
int Main::main() {
// code
return 0;
}
And MojSwiat2.h:
class Main {
public:
int main();
};
Main run;
int Main::main() { // with this I have error: function int Main::main(void) already has a body
run.main();
} // and without I got unresolved external symbol _main referenced in function __tmainCRTStartup
Reason I need to do this: Accessing protected members of class from main
Solução
By defining a normal main
that only contains a call to your other function. Like this:
int main(int, char**) {
return Main().main();
}
Outras dicas
int main(int argc, char* argv[])
{
Main m;
return m.main();
}
or if Main::main is declared static
int main(int argc, char* argv[])
{
return Main::main();
}
You still need to define main
.
class my_app {
int main(int argc, char* argv[])
{
// ...
}
}
my_app app;
int main(int argc, char *argv[])
{
return app.main(argc, argv);
}