1

When I try to compile the second file in Visual Studio 2010, it gives the error of multiply defined symbols, which makes sense since it compiles both and thus includes iostream twice, causing "one or more multiply defined symbols found" error to come up. So, I thought maybe I could use include guards to prevent this, but as I understand it, you must add the guards to files that you are including. What is the proper course of action here?

1.cpp

#include <iostream>

using namespace std;

int main()
{
    int x;

    cout << "**********************************" << endl
         << "*    Programming Assignment 1    *" << endl
         << "*     Computer Programming I     *" << endl
         << "*       Author: Cedar Mora       *" << endl
         << "*   Due Date: Thursday, Jan. 24  *" << endl //this isn't true
         << "**********************************" << "\n" << endl;

    cout << "press any key" <<endl;
    getchar();

    return 0;
}

2.cpp

#include <iostream>

using namespace std;

int main()
{
    cout << "bla" << endl;

    return 0;
}

As a side note, is it possible just to compile one item of a project?

I wouldn't normally have asked, I'd just google the answer and there it would be, but I can't seem to make sense of this one. It's forcing me to have to open a separate project for each portion of code I write in order to make it compile.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Cedar Mora
  • 13
  • 4
  • 7
    You can definitely include `iostream` more than once in a single translation unit. Your problem is having two `main` functions. – chris Jul 31 '14 at 03:29
  • I figured it was something simple like that. So I just need to change the name of my function then? Also, I didn't expect an answer that fast, thank you for the prompt help. – Cedar Mora Jul 31 '14 at 03:34
  • 3
    Even if you change the function name, one will not run because you didn't call it anywhere. Each project must have only one main function as the entry. If you need many multiple mains, create multiple projects in a single solution – phuclv Jul 31 '14 at 03:36
  • I'm using a textbook to learn C++ but it really doesn't help you for these kinds of practical things. It assumes that I couldn't understand the complex concepts up front like namespace, #include or main(), and tells me that they'll explain them later. Then I have these kinds of problems with the advanced concepts and since they haven't explained them, I have no place to even start thinking of a solution. It's extremely frustrating. – Cedar Mora Jul 31 '14 at 04:13

2 Answers2

1

As others have noted, your issue is simply having two main functions. main is a special function in C++ (and Java, and C#...) which tells the computer to start execution there. Think like a computer--if you can only execute one line at a time, where would you start? If you start in 1.cpp, for example, how would you ever know to jump to the code in 2.cpp? To execute code in another function, you must call it--otherwise, the computer will execute the code contained in main and then terminate.

dhganey
  • 113
  • 1
  • 6
  • This explanation doesn't actually make sense. you jump from 1 to 2 because 1 calls the function 2_bar. issue is ambiguous entry point. – djechlin Jul 31 '14 at 04:03
  • I'm not sure what you mean. In OP's code, 1 does not make any calls to 2, because the only function in 2 is `main`... You're right, the issue is ambiguous entry point--my only point is to look at it line by line and try to think through how the code in another `main` function would ever be reached. – dhganey Aug 01 '14 at 04:08
0

you are not supposed to have two main functions! change one of them to something else. for example, try changing the one in 2.cpp to int test(). also, no matter what order of the functions you have, it will always kick off with running main function even if you put it at the end of the file.

try this, in 1.cpp

#include"2.h"
int main()
{
   int x = test();

   return 0;
}

then add a header file, 2.h

#ifndef 2_H_
#define 2_H_

int test();

#endif

and, in the 2.cpp file, you put the implemetation there:

#include"2.h"
int test(){
cout << "bla" << endl;

    return 0;
}

the purpose of 2.h and 2.cpp is to separate the declaration and implementation.

  • So for example, I could rename 2.cpp to int test() and then run it inside main() by calling it? – Cedar Mora Jul 31 '14 at 04:06
  • more info added. check it out. hope it helps u out –  Jul 31 '14 at 04:15
  • Now it compiles and runs both, with 1.cpp first and 2.cpp second. Is it possible to just compile and run 2.cpp? I'm currently testing your example. – Cedar Mora Jul 31 '14 at 04:19
  • exclude it from the project/build, i guess –  Jul 31 '14 at 04:20
  • Okay, I've got a lot of basic questions that I really appreciate you answering because I'm new and don't really have anybody I can talk with about this so even if it's very easy for you, I want you to know that your help is extremely appreciated. – Cedar Mora Jul 31 '14 at 04:23
  • Where is the executable file to run the program? – Cedar Mora Jul 31 '14 at 04:23
  • Also, what is the difference between .h files and .cpp files? – Cedar Mora Jul 31 '14 at 04:26
  • What is the difference between the header and the um, whatever you call it, body I suppose. – Cedar Mora Jul 31 '14 at 04:27
  • http://stackoverflow.com/questions/875479/what-is-the-difference-between-a-cpp-file-and-a-h-file –  Jul 31 '14 at 04:27
  • correct. i think u would learn a lot as u spend more time on this website, lol –  Jul 31 '14 at 04:32
  • Can you include just the header from a file which also has a body? – Cedar Mora Jul 31 '14 at 04:32
  • Trust me, I've been a lurker for about a month now trying to learn python and C++. I haven't had to ask a question yet because somebody had usually asked a similar enough question already that I didn't need to actually join stackoverflow. But now I have. – Cedar Mora Jul 31 '14 at 04:34
  • `2_H_` is not a legal identifier, it has to start with alpha or underscore – M.M Jul 31 '14 at 05:49