tags:

views:

549

answers:

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

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

I'm getting following error : expected unqualified-id at end of input main.cpp Problem

Is there any solution

+17  A: 

main is just main simply! a function:

class EntranceClass
{
...
};

int main()
{

}
AraK
It's worth emphasizing the need for the semicolon at the end of the class declaration.
luke
+1  A: 

You are looking for Constructor of object?:)

Anyway try remove static ...

Rin
+7  A: 

Have you maybe forgotten the semicolon after the closing paranthesis?

RED SOFT ADAIR
+5  A: 

The error is referring to the use of static keyword before class definition - compiler expects a variable name after that (as in C++ there is no such thing as 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.

PiotrLegnica
You don't tell it to the linker. What you do is something like "int main() { return EntranceMain::main(); }" at global level. Otherwise, you're writing an unportable hack (which may go away with any change to the implementation) instead of C++.
David Thornley
main, is a bit generic for a method name.
call me Steve
`main` was already mentioned by others, this is just another way. I didn't say it's the best; and if you think the wording is bad, feel free to edit.
PiotrLegnica
Its not true, that there are no static methods in C++. A classmember as i.e. static int myFunction() is perfectly legal.
RED SOFT ADAIR
I know. I said "static class".
PiotrLegnica
+2  A: 

This seems like horrible coding style to me to be putting your main into a class, but if you really wanted to do it I think the implementation would be more like so:

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

Foo bar;

int Foo::main(){
bar.main();
}

As I said though, this seems to be very bad coding style. You're better off building your class in a .hpp file then linking it into your program_main.cpp (via #include "foo.hpp") which has the int main(){ return 0; } function for you to call the class.

Jesse O'Brien
Agreed - woe to the person who has to then maintain that program after this programmer has left the effort.
MBillock
+1  A: 
Dima
You may want to add a note about the alternate main prototype: "int main()".
Bill
+5  A: 

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

Section 3.6.1, paragraph 3: "The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation-defined. A program that declares main to be inline or static is ill-formed. The name main is not otherwise reserved. [Example: member function, classes, and enumerations can be called main, as can entities in other namespaces.]"

This means that, by declaring a member function main, you're just declaring a member function. It has no special significance, and doesn't mean anything in the class can be called independently. The program would mean entirely the same thing if you substituted snicklefrazz for that function name and all references.

Stylistically, snicklefrazz would be better, since it wouldn't lead to any possible conclusion with the standard main function.

Section 3.6.1, paragraph 1: "A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function."

This means that, unless you're writing in what the standard calls a freestanding environment (typically used in writing embedded systems), you need to define a main global function, and this is where the program starts.

In Java, a class can have a main method, which is where the program begins when the class is invoked. This is not the case in C++, and there is no way to accomplish this directly.

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

David Thornley
A: 

One trick you can do is to create a class that is called something like "BaseApp" that is derived from, and then implement a virtual function that calls through to the real version.

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

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

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

SpecializedApp g_App;

Then, you have main() that calls through to it:

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

This is a way that you can have main be a type of generic startup routine that can call into different entry points depending on how things are linked in. You could have a Win32 class or a standard C class on top of BaseApp.

A static member function cannot use 'this' since it operates on no instance at all.
Luc Touraille