Reputation: 167
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
Upvotes: 1
Views: 2176
Reputation: 3713
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();
}
Upvotes: 1
Reputation: 2301
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);
}
Upvotes: 0
Reputation: 11116
By defining a normal main
that only contains a call to your other function. Like this:
int main(int, char**) {
return Main().main();
}
Upvotes: 2