如何在 C++ 中的类中实现 main 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1504205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I implement main method in a class in C++?
提问by Hymanie
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
我收到以下错误:在输入 main.cpp 问题的末尾预期不合格的 id
Is there any solution
有没有解决办法
回答by AraK
main
is just main
simply! a function:
main
仅仅是main
简单的!一个功能:
class EntranceClass
{
...
}; //<-- note the semicolon
int main()
{
}
回答by Cat Plus Plus
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
).
错误是指static
在类定义之前使用关键字 - 编译器期望在此之后使用变量名(如在 C++ 中没有这样的东西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).
如果您想将其static int EntranceMain::main(void)
用作程序的入口点,那么一种方法是将其告知您的链接器,即为其提供该函数的完整修饰名称。这在很大程度上取决于您使用的编译器和链接器,因此您需要参考它们的文档。但是使用它可能意味着您需要包含启动代码(例如 CRT 初始化)。
Note that this is not so standard-compliant, though.
请注意,这并不符合标准。
回答by David Thornley
According to the standard, you're not writing a true main
function.
根据标准,您不是在编写真正的main
函数。
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.]"
第 3.6.1 节,第 3 段:“该函数main
不得在程序中使用 (3.2)。的链接 (3.5)main
是实现定义的。声明main
为inline
或格式错误的程序static
。名称main
不是其他保留。[示例:可以调用成员函数、类和枚举main
,就像其他命名空间中的实体一样。]”
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.
这意味着,通过声明一个成员函数main
,您只是在声明一个成员函数。它没有特别的意义,并不意味着类中的任何东西都可以独立调用。如果您替换snicklefrazz
该函数名称和所有引用,该程序的含义将完全相同。
Stylistically, snicklefrazz
would be better, since it wouldn't lead to any possible conclusion with the standard main
function.
在风格上,snicklefrazz
会更好,因为它不会导致标准main
功能的任何可能的结论。
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."
第 3.6.1 节,第 1 段:“程序应包含一个名为 的全局函数main
,它是程序的指定开始。是否需要独立环境中的程序来定义main
函数是实现定义的。”
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.
这意味着,除非您在标准所称的独立环境(通常用于编写嵌入式系统)中编写代码,否则您需要定义一个main
全局函数,这就是程序开始的地方。
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.
在 Java 中,一个类可以有一个main
方法,这是调用该类时程序开始的地方。在 C++ 中不是这种情况,并且没有办法直接实现这一点。
(And, as others have mentioned, a class
cannot be static
, and a class definition ends with a semicolon.)
(并且,正如其他人所提到的, aclass
不能是static
,并且类定义以分号结尾。)
回答by RED SOFT ADAIR
Have you maybe forgotten the semicolon after the closing paranthesis?
您是否忘记了右括号后的分号?
回答by IProblemFactory
You are looking for Constructor
of object?:)
你在找Constructor
对象?:)
Anyway try remove static
...
无论如何尝试删除static
...
回答by Jesse O'Brien
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:
将您的 main 放入一个类中对我来说似乎是一种可怕的编码风格,但是如果您真的想这样做,我认为实现会更像这样:
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.
正如我所说,这似乎是非常糟糕的编码风格。您最好在 .hpp 文件中构建您的类,然后将其链接到具有 int main(){ return 0; 的 program_main.cpp(通过 #include "foo.hpp")。函数供您调用该类。
回答by Dima
In C++ you can have "global" functions, i. e. functions that are not members ("methods" in Java lingo) of any class.
By default the entry point to a C++ program is a "global"
main()
function that in general looks like this:
在 C++ 中,您可以拥有“全局”函数,即不是任何类的成员(Java 术语中的“方法”)的函数。
默认情况下,C++ 程序的入口点是一个“全局”
main()
函数,通常如下所示:
int main(int argc, char *argv[])
{
// do stuff
}
The arguments to main()
allow your program to take command line arguments. They can be omitted like so
该参数main()
让你的程序采取命令行参数。它们可以像这样省略
int main()
{
// do stuff
}
in which case your program does not take any arguments. When a program exits normally, the convention is for main()
to return 0. A return value other than 0 is typically used to indicate an error.
在这种情况下,您的程序不接受任何参数。当程序正常退出时,约定是main()
返回 0。非 0 的返回值通常用于指示错误。
There is no notion of a static class in C++. You can have static member functions and data members, or static global variables (different meaning of static inherited from C).
You need a semicolon after the class definition.
C++ 中没有静态类的概念。你可以有静态成员函数和数据成员,或者静态全局变量(从C继承的静态的不同含义)。
在类定义之后需要一个分号。
回答by Dima
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.
您可以做的一个技巧是创建一个名为“BaseApp”之类的类,该类派生自,然后实现一个调用真实版本的虚函数。
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.
然后从它派生一个类,实现一个 EntryPoint(),并定义你的对象。
class SpecializedApp: public BaseApp
{
public:
void EntryPoint() { ... }
};
SpecializedApp g_App;
Then, you have main() that calls through to it:
然后,您有 main() 调用它:
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.
这是一种可以让 main 成为一种通用启动例程的方法,它可以根据事物的链接方式调用不同的入口点。您可以在 BaseApp 之上拥有 Win32 类或标准 C 类。