简体   繁体   English

int main()首先在这里定义错误

[英]int main() first defined here errors

I just started learning c++ today by doing Project Euler problems. 我今天刚开始通过做Euler项目问题​​来学习c ++。 My issue is that in Java, I can organize the problems into their own packages, but how do I do that in c++? 我的问题是,在Java中,我可以将问题组织到自己的程序包中,但是如何在c ++中实现呢?

The image below shows my Java/c++ setup. 下图显示了我的Java / c ++设置。 The Java part works perfectly for me, however, I can't do the same in c++ (using folders and namespaces) without getting "multiple definition of main" errors. Java部分非常适合我,但是,在c ++中(使用文件夹和名称空间),我不能做同样的事情而不会出现“主要定义多重”错误。

在此处输入图片说明

How can I fit all the individual problems inside the "Project Euler C++" folder? 如何解决“ Project Euler C ++”文件夹中的所有单个问题? Thanks 谢谢

Edit: After hours of searching, I found that this post had the best and easiest solution for my problem 编辑:经过数小时的搜索,我发现这篇文章对我的问题有最好,最简单的解决方案

In c and c++ you can only have one main() function. 在c和c ++中,您只能有一个main()函数。 I'm guessing the problem here is that Problem1.cpp and Problem2.cpp are in the same project, but each have a main() function. 我猜这里的问题是Problem1.cpp和Problem2.cpp在同一个项目中,但是每个都有一个main()函数。 Look up how to use header files, and then include the header files in your main program. 查找如何使用头文件,然后将头文件包含在主程序中。

Your problem has little to do with C++, and much to do with Eclipse (nb the NetBeans IDE is no better, and I'm sure plenty of others too). 您的问题与C ++无关,而与Eclipse密切相关(nb NetBeans IDE并没有更好,我敢肯定还有很多其他问题)。 You've got a single Eclipse project, but multiple definitions of main() , each of which needs to go in its own executable file. 您只有一个Eclipse项目,但是main()多个定义,每个定义都需要放入其自己的可执行文件中。

See here for more details and some suggestions: Project with multiple binaries in Eclipse CDT 请参阅此处以获取更多详细信息和一些建议: 在Eclipse CDT中具有多个二进制文件的项目

You could also write a single main() which dispatches to multiple other "sub-main" functions based on , say, the first command-line argument. 您还可以编写一个main() ,它根据第一个命令行参数分配给其他多个“ sub-main”函数。 Some programs behave this way, including many version control systems (eg git add and git commit which both invoke one program but then farm out the work based on the first argument). 一些程序会以这种方式运行,包括许多版本控制系统(例如git addgit commit ,它们都调用一个程序,然后根据第一个参数来分配工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM