Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement main method in a class in C++?

Tags:

c++

Consider:

static class EntranceClass {
public:
    static void RegisterSomething()
    {
    }

    static int main()
    {
        RegisterSomething();
        return 0;
    }
} // <-- Expected unqualified-id at the end

I'm getting the following error:

expected unqualified-id at end of input main.cpp Problem

Is there a solution?

like image 554
jack-london Avatar asked Oct 01 '09 14:10

jack-london


2 Answers

main is just main. It is simply a function:

class EntranceClass
{
    ...
}; // <-- Note the semicolon

int main()
{

}
like image 107
Khaled Alshaya Avatar answered Sep 30 '22 05:09

Khaled Alshaya


The error is referring to the use of the static keyword before the class definition - the compiler expects a variable name after that (as in C++ there is no such thing as a static class).

And if you want to use static int EntranceMain::main(void) as your program's entry point, then one way to do it is to tell that to your linker, i.e., give it a full, decorated name of that function. This is highly dependent on which compiler and linker you use, so you need to refer to their documentation. But using that will probably mean you need to include the startup code (e.g., CRT initialisation).

Note that this is not so standard-compliant, though.

like image 32
Cat Plus Plus Avatar answered Sep 30 '22 04:09

Cat Plus Plus