C++ “主要”的多种定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12871157/
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
Multiple definitions of "Main"
提问by Jsp1304
In the journey to learning C++ im learning through the C++ Manual thats on the actual website. Im using DevC++ and have hit a problem, not knowing whether its the compilers error or not.
在学习 C++ 的过程中,我通过实际网站上的 C++ 手册进行学习。我使用 DevC++ 并且遇到了问题,不知道它是否是编译器错误。
I was going through this code bit by bit typing it in myself, as I feel its more productive, and adding my own stuff that ive learnt to the examples, then I get to initialising variables. This is the code that is in the C++ manual
我在自己的代码中一点一点地输入它,因为我觉得它更有效率,并将我自己学到的东西添加到示例中,然后我开始初始化变量。这是 C++ 手册中的代码
#include <iostream>
using namespace std;
int main ()
{
int a=5; // initial value = 5
int b(2); // initial value = 2
int result; // initial value undetermined
a = a + 3;
result = a - b;
cout << result;
return 0;
}
This is popping up a compiler error saying " Multiple definitions of "Main"" Now This is on the actual C++ page so im guessing its a compiler error.
这是弹出一个编译器错误,说“多个“主”的定义”现在这是在实际的 C++ 页面上,所以我猜测它是一个编译器错误。
Could someone please point me in the right direction as to why this is happening and what is the cause for this error.
有人可以指出我为什么会发生这种情况以及导致此错误的原因的正确方向。
回答by CrazyCasta
Multiple definitions of "main" suggests that you have another definition of main. Perhaps in another .c or .cpp file in your project. You can only have one function with the same name and signature (parameter types). Also, main is very special so you can only have one main function that can be used as the entry point (has either no parameters, one int, or an int and a char**) in your project.
“main”的多个定义表明您对 main 有另一个定义。也许在您项目中的另一个 .c 或 .cpp 文件中。您只能拥有一个具有相同名称和签名(参数类型)的函数。此外,main 非常特殊,因此您的项目中只能有一个可用作入口点的 main 函数(没有参数,一个 int,或一个 int 和一个 char**)。
P.S. Technically this is a linker error. It's a subtle difference, but basically it's complaining that the linker can't determine which function should be the entry point, because there's more than one definition with the same name.
PS 从技术上讲,这是一个链接器错误。这是一个细微的区别,但基本上它在抱怨链接器无法确定哪个函数应该作为入口点,因为有多个具有相同名称的定义。