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

static class EntranceClass { public: static void RegisterSomething() { } static int main() { RegisterSomething(); return 0; } } // <-expected unqualified-id at end 

I get the following error: expected unqualified identifier at the end of main.cpp input

Is there any solution

+6
c ++
source share
8 answers

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

And if you want to use static int EntranceMain::main(void) as the entry point to the program, then one way to do this is to inform your linker about it, i.e. give it the full, decorated name of this function. It depends a lot on which compiler and linker you are using, so you need to consult their documentation. But using this probably means you need to include a startup code (e.g. CRT initialization).

Please note that this is not so standard, however.

+8
source share

main just main easy! function:

 class EntranceClass { ... }; //<-- note the semicolon int main() { } 
+21
source share

According to the standard, you are not writing a true main function.

Section 3.6.1, paragraph 3: "The main function should not be used (3.2) inside the program. The connection (3.5) from main is determined by the implementation. The program declaring main to be inline or static is incorrect. The name main not otherwise reserved. [Example: member function, classes, and enumerations can be called by main , as well as objects in other namespaces.] "

This means that by declaring a member function main , you simply declare a member function. This is not significant and does not mean that anything in the class can be called independently. The program will mean exactly the same if you replaced snicklefrazz with this function name and all links.

Stylistically, snicklefrazz would be better since it would not lead to any possible output with the standard main function.

Section 3.6.1, paragraph 1: "The program must contain a global function named main , which is the designated start of the program. It is determined by the implementation whether the program is required in a stand-alone environment to determine main ."

This means that if you do not write what the standard calls a standalone environment (usually used when writing embedded systems), you need to define the global function main , and this is where the program starts.

In Java, a class can have a main method in which the program starts when the class is called. This does not apply to C ++, and there is no direct way to accomplish this.

(And, as others have noted, a class cannot be static , and a class definition ends with a semicolon.)

+9
source share

Did you forget the semicolon after closing the parantes?

+7
source share

Are you looking for a Constructor object? :)

In any case, try removing static ...

+1
source share

It seems to me that this is a terrible coding style that I put into the class, but if you really wanted it, I think the implementation will be more like:

 class Foo{ public: int main(){ return 0; }; }; Foo bar; int Foo::main(){ bar.main(); } 

As I said, this is apparently a very bad coding style. You better create your class in a .hpp file and then link it to your program_main.cpp (via #include "foo.hpp"), which has int main () {return 0; } to call the class.

+1
source share
  • In C ++, you can have "global" functions, i.e. e. functions that are not members ("methods" in Java lingo) of any class.

  • By default, the C ++ program entry point is a "global" main() function, which generally looks like this:

 int main(int argc, char *argv[]) { // do stuff } 

The main() arguments allow your program to accept command line arguments. They may also be omitted.

 int main() { // do stuff } 

and in this case your program does not accept any arguments. When the program exits normally, the convention is returned for main() to return 0. To indicate an error, a return value other than 0 is usually used.

  • In C ++, there is no concept of a static class. You can have static member functions and data members, or static global variables (different values ​​of static inherited from C).

  • You will need a semicolon after the class definition.

+1
source share

One trick you can do is create a class called something like β€œBaseApp” that is derived from, and then implement a virtual function that calls the real version.

 class BaseApp { public: virtual void EntryPoint() = 0; static BaseApp* GetApp() { return this; } }; 

Then just derive a class from it, implement EntryPoint (), and define your object.

 class SpecializedApp: public BaseApp { public: void EntryPoint() { ... } }; SpecializedApp g_App; 

Then you have main () that accesses it:

 void main() { BaseApp::GetApp()->EntryPoint(); } 

This is the way that you can be the main type of universal start-up routine that can trigger different entry points depending on how things are connected. You can have a Win32 class or a standard C class on top of BaseApp.

0
source share

All Articles