0

I just started learning c++ today by doing Project Euler problems. My issue is that in Java, I can organize the problems into their own packages, but how do I do that in c++?

The image below shows my Java/c++ setup. 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.

enter image description here

How can I fit all the individual problems inside the "Project Euler C++" folder? Thanks

Edit: After hours of searching, I found that this post had the best and easiest solution for my problem

Haque1
  • 575
  • 1
  • 11
  • 21

2 Answers2

6

In c and c++ you can only have one main() function. I'm guessing the problem here is that Problem1.cpp and Problem2.cpp are in the same project, but each have a main() function. Look up how to use header files, and then include the header files in your main program.

Nathan
  • 73,987
  • 14
  • 40
  • 69
3

Your problem has little to do with C++, and much to do with Eclipse (n.b. the NetBeans IDE is no better, and I'm sure plenty of others too). You've got a single Eclipse project, but multiple definitions of main(), each of which needs to go in its own executable file.

See here for more details and some suggestions: Project with multiple binaries in 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. Some programs behave this way, including many version control systems (e.g. git add and git commit which both invoke one program but then farm out the work based on the first argument).

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436