C++ 编译时错误:'main' 的多重定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8893093/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:17:18  来源:igfitidea点击:

Compile-time error: Multiple definition of 'main'

c++

提问by sandbox

I am getting the following error: Multiple definition of `main'

我收到以下错误:“main”的多重定义

I have created a new project, there are two c++ files in it:

我新建了一个项目,里面有两个c++文件:

File 1

文件 1

#include <iostream>

 using namespace std;

int main()
{
    cout<<"Hello World";
    //fflush(stdin);
    //getchar();
    return 0;
}

File 2

档案 2

#include <iostream>

using namespace std;

int main()
{
    cout<<"Demo Program";
    return 0;
}

When I press Build project and Run, I get error. How do I run these files?

当我按 Build project 和 Run 时,出现错误。我如何运行这些文件?

回答by MrKiane

You cannot have two main functions in the same project. Put them in separate projects or rename one of the functions and call it from the other main function.

同一项目中不能有两个主要功能。将它们放在单独的项目中或重命名其中一个函数并从另一个主函数调用它。

You can never have more than one main() function in your project since it is the entrypoint, no matter what the parameter list is like.

无论参数列表是什么样的,您的项目中都不能有多个 main() 函数,因为它是入口点。

You can however have multiple declarations of other functions as long as the parameter list is different (function overloading).

但是,只要参数列表不同(函数重载),您就可以有其他函数的多个声明。

File 1

文件 1

#include <iostream>

using namespace std;

int main()
{
    cout<<"Hello World";
    otherFunction();
    return 0;
}

File 2

档案 2

#include <iostream>

using namespace std;

void otherFunction()
{
    cout<<"Demo Program";
}

Dont forget the appropiate #includes.

不要忘记适当的#includes。

回答by atoMerz

You can't have two main functions. In fact you can't have any two functions having the same signature through out your project (not your files).
And as Mr.TAMER said main is a special case, you can't even have two functions called main.

你不能有两个主要功能。事实上,在整个项目(而不是文件)中,不能有任何两个具有相同签名的函数。
正如 TAMER 先生所说, main 是一个特例,您甚至不能将两个函数称为main

回答by Azodious

  1. Decide which file you want to be as entry point of your project.

  2. In other file, change the method name to some other name. you can call it from the file you chose at step 1.

  1. 决定要作为项目入口点的文件。

  2. 在其他文件中,将方法名称更改为其他名称。您可以从您在步骤 1 中选择的文件中调用它。

mainis entry point of your program and you can't have more than one entry point.

main是您程序的入口点,并且您不能有多个入口点。

For more clear explanation see this: Two 'main' functions in C/C++

有关更清晰的解释,请参见:C/C++ 中的两个“主要”函数

回答by vikky

You can not use the same function signature in a same project,becouse the compiler start execution from the main(). If you define multiple times of main() then it produce an error.

你不能在同一个项目中使用相同的函数签名,因为编译器从 main() 开始执行。如果您多次定义 main() ,则会产生错误。

file1.c

文件1.c

#include <iostream>

#include <file2.h>
using namespace std;

int main()
{
cout<<"Hello World";
//fflush(stdin);
//getchar();
return 0;
}

And in file2.h,you can define the function of file2.c(first rename the main() of the file2)

而在file2.h中,可以定义file2.c的函数(先重命名file2的main())