问题描述
我得到以下错误:`main'的多重定义
I am getting the following error: Multiple definition of `main'
我创建了一个新项目,其中有两个c ++文件:
I have created a new project, there are two c++ files in it:
文件1
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
//fflush(stdin);
//getchar();
return 0;
}
档案2
#include <iostream>
using namespace std;
int main()
{
cout<<"Demo Program";
return 0;
}
当我按Build项目和运行时,如何运行这些文件?
When I press Build project and Run, I get error. How do I run these files?
推荐答案
在同一个项目中不能有两个主要功能。将它们放在单独的项目中或重命名其中一个函数并从其他主函数调用它。
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.
您的项目中永远不能有多个main
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.
然而,只要参数列表不同,你可以有多个其他函数的声明()。
You can however have multiple declarations of other functions as long as the parameter list is different (function overloading).
文件1
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
otherFunction();
return 0;
}
档案2
#include <iostream>
using namespace std;
void otherFunction()
{
cout<<"Demo Program";
}
不要忘记适当的#includes。
Dont forget the appropiate #includes.
这篇关于编译时错误:'main'的多重定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!