468,539 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

Multiple "int Main()" errors(NEED HELP!)

90 64KB
Now I'm learning while coding so to speak and I have an error I haven't got any idea on how to fix. The file is basicly a documantary of what I learn and how I picture it in my head, so if I forget, I can go back and look...

My errors:

1. "In function `int main()':"

2. "Redefinition of `int main()'" (I know that it sets
the value of Main again, but how can I fix it?)

3. "`int main()' previously defined here" (same as #2
but how can I fix it?)

My Code:
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. #ifndef _AndOrStatement_cpp_
  5. #define _AndOrStatement_cpp_
  6.  
  7. #include "LoopsCppTutorial.cpp"
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.     //Press "Compile & Run" to see what they output
  14.  
  15.     cout << "NOT: " << endl;
  16.  
  17.     //This equals 1 = false
  18.     double var9;
  19.     if (1 != 0)
  20.     {
  21.         var9 = true;
  22.         cout << var9 << endl;
  23.     }
  24.     else
  25.     {
  26.         var9 = false;
  27.         cout << var9 << endl;
  28.     };
  29.  
  30.     //__________________________________________________________
  31.  
  32.     cout << "AND: " << endl;
  33.  
  34.     //This equals 0 = true
  35.     double var;
  36.     if (0&&0) 
  37.     {
  38.         var = true;
  39.         cout << var << endl;
  40.     }
  41.     else 
  42.     {
  43.         var = false;
  44.         cout << var << endl;
  45.     };
  46.  
  47.     //__________________________________________________________
  48.  
  49.     //This equals 1 = false
  50.     double var2;
  51.     if (1&&1) 
  52.     {
  53.         var2 = true;
  54.         cout << var2 << endl;
  55.     }
  56.     else 
  57.     {
  58.         var2 = false;
  59.         cout << var2 << endl;
  60.     };
  61.  
  62.     //__________________________________________________________
  63.  
  64.     //This equals 0 = true
  65.     double var3;
  66.     if (1&&0) 
  67.     {
  68.         var3 = true;
  69.         cout << var3 << endl;
  70.     }
  71.     else 
  72.     {
  73.         var3 = false;
  74.         cout << var3 << endl;
  75.     };
  76.  
  77.     //__________________________________________________________
  78.  
  79.     //This equals 0 = true
  80.     double var4;
  81.     if (0&&1) 
  82.     {
  83.         var4 = true;
  84.         cout << var4 << endl;
  85.     }
  86.     else 
  87.     {
  88.         var4 = false;
  89.         cout << var4 << endl;
  90.     };
  91.  
  92.     //__________________________________________________________
  93.  
  94.     cout << "OR: " << endl;
  95.     //This equals 0 = true
  96.     double var5;
  97.     if (0||0)
  98.     {
  99.         var5 = true;
  100.         cout << var5 << endl;
  101.     }
  102.     else
  103.     {
  104.         var5 = false;
  105.         cout << var5 << endl;
  106.     };
  107.  
  108.     //__________________________________________________________
  109.  
  110.     //This equals 1 = false
  111.     double var6;
  112.     if (1||1)
  113.     {
  114.         var6 = true;
  115.         cout << var6 << endl;
  116.     }
  117.     else
  118.     {
  119.         var6 = false;
  120.         cout << var6 << endl;
  121.     };
  122.  
  123.     //__________________________________________________________
  124.  
  125.     //This equals 1 = false
  126.     double var7;
  127.     if (0||1)
  128.     {
  129.         var7 = true;
  130.         cout << var7 << endl;
  131.     }
  132.     else
  133.     {
  134.         var7 = false;
  135.         cout << var7 << endl;
  136.     };
  137.  
  138.     //__________________________________________________________
  139.  
  140.     //This equals 1 = false
  141.     double var8;
  142.     if (1||0)
  143.     {
  144.         var8 = true;
  145.         cout << var8 << endl;
  146.     }
  147.     else
  148.     {
  149.         var8 = false;
  150.         cout << var8 << endl;
  151.     };
  152.  
  153.     //__________________________________________________________
  154.  
  155.     system("PAUSE");
  156.     return EXIT_SUCCESS;
  157. }
  158.  
  159. #endif
  160.  
This is a "Console Application" written in Dev-C++

X(sorry for bad english, I'm norwegian and that can explain a lot... )
Jul 4 '13 #1
6 4919
Nepomuk
3,112 Expert 2GB
The code you've written looks fine, but in line 7 you include another file:
Expand|Select|Wrap|Line Numbers
  1. #include "LoopsCppTutorial.cpp"
If you remove that like, the code works without any problems. So I'm guessing that "LoopsCppTutorial.cpp" includes a main method?

One way to solve all of this would be to split your learning project further. Have you learned about header files and prototypes yet? If so, you could have a header file for this and every other step you take and none of your cpp files contain main methods. (Just call them something different.) Then you create one Main.cpp which would look something like this:
Expand|Select|Wrap|Line Numbers
  1. #include "step1.h"
  2. #include "step2.h"
  3. #include "step3.h"
  4.  
  5. int main() {
  6.    // The main method from step 1 is now called "step1()":
  7.    step1();
  8.    // Similarly for step 2:
  9.    step2();
  10.    // And of course for step 3:
  11.    step3();
  12.    return 0;
  13. }
  14.  
Now when you compile your project it would have to include the other cpp files too. I'm not sure how Dev-C++ solves this (it might be automatic) but with g++ I would do something like this:
Expand|Select|Wrap|Line Numbers
  1. g++ Main.cpp step1.cpp step2.cpp step3.cpp
Jul 4 '13 #2
Xillez
90 64KB
yes, the "#include "LoopsCppTutorial.cpp"" includes a main method and that is the problem?

ok, and no I haven't learned about hearder files(I know why they are there and maybe what their task are but haven't learned the language so I can't write one... )

I know the code works without the #include thing because I used the "Compile & Run" option, but do I really need a header file? and if so how am I supposed to write it...
Jul 5 '13 #3
weaknessforcats
9,207 Expert Mod 8TB
First, you never include code. That is, you never include anything that will occupy memory. As you can see, you get multiple definition errors.

Second, you don't need header files.

However, if you need to call a function in several.cpp files, you can't have the code in each file so you write the code once in one file and in the other files you insert the first line of the function followed by a semi-colon. This is a "function prototype" and it tells the compiler not to worry if the function you call is not defined (has no code) in this file.

If your program has 500 files, you will need to have this prototype in 499 files. If you change the function so that the first line is different, you have changed the prototype. So you need to make 499 other changes.---OR you have the prototype in one file that you #include in the other 499. This way you make your change once and rebuild your program.

A header file, then, is just a text file like your source code files. There's no particular magic in the name of the file. C uses .h extensions historically while C++ does not.
Jul 5 '13 #4
Xillez
90 64KB
ok, so I need to learn how to write the header files/learn how to join two or more files together?
Jul 5 '13 #5
weaknessforcats
9,207 Expert Mod 8TB
Yes you do.

The process involves a makefile which contains info on the files to compile, instructions to the linker for combining all of the object files t make your .exe.

Depending upon your IDE, the makefile may be generated for you when you click build on a project.

The make utility reads the makefile and launches the various processes. This is the build.
Jul 5 '13 #6
Xillez
90 64KB
ok, I'll get right on it... thanks for the help...

X
Jul 5 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Bob Rundle | last post: by
3 posts views Thread by JFB | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.