我收到以下错误:`main’的多重定义
我创建了一个新项目,里面有两个c文件:
档案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;
- }
解决方法
您不能在同一个项目中拥有两个主要功能.将它们放在单独的项目中或重命名其中一个函数,并从另一个main函数调用它.
您的项目中永远不能有多个main()函数,因为它是入口点,无论参数列表是什么样的.
但是,只要参数列表不同(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.