Multiple files/Compilation error

Hello all,

When compiling the following files, the error that I get is:

In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&,
ArgSequence const&)': multiple definition first defined here error.

The files are as follows:

ansi.h header file with declarations and defintions of inline functions for
ansi escape sequences such as background colors and cursor movements.

homescreen.h definition of class Homescreen
homescreen.cpp definitions of class Homescreen functions

scoreboard.h definition of class Scoreboard
scoreboard.cpp definitions of class Scoreboard functions

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//begin file: homescreen.h
#ifndef HOMESCREEN_H
#define HOMESCREEN_H

class Homescreen{
   class functions...
};

#endif
//end file homescreen.h

//begin file: homescreen.cpp
#include <iostream>
#include "homescreen.h"
#include "ansi.h"
using namespace std;

//definitions of class Homescreen functions...

//end file: homescreen.cpp

//begin file: scoreboard.h
#ifndef SCOREBOARD_H
#define SCOREBOARD_H

class Scoreboard{
   class functions...
};

//end file: scoreboard.h

//begin file: scoreboard.cpp
#include <iostream>
#include "scoreboard.h"
#include "ansi.h"
using namespace std;

definitions of class scoreboard functions...

//end file: scoreboard.cpp

//begin file: main.cpp
#include <iostream>
#include "homescreen.h"
#include "scoreboard.h"
#include "ansi.h"

int main()
{

   //plays the game to be built

   return 0;
}

//end file: main.cpp

//The previous files are compiled using a makefile. The makefile is as follows:

all: game

homescreen.o: homescreen.cpp homescreen.h
       g++ -c homescreen.cpp

scoreboard.o: scoreboard.cpp scoreboard.h
       g++ -c scoreboard.cpp

main.o: main.cpp
       g++ -c main.cpp

game: main.o homescreen.o scoreboard.o
       g++ -o game main.o homescreen.o scoreboard.o

clean:
       rm -f *.o game


After trying many differet approaches to fix the compilation error and reading many forums, to no avail, has anything worked. Yes, this is homework question for a data structures class, however, I do not want any code, just possibly any explanation as to where the error is originating from. Thank you for any and all help.
Where is that operator<<(std::basic_ostream<char, std::char_traits<char> >&, ArgSequence const&)? (The code that you show does not reveal that.)
Last edited on
http://www.eelis.net/iso-c++/testcase.xhtml
A testcase is said to reproduce the problem if, when we independently try to compile and run the testcase locally, we get the same compiler error (or our compiler's version of it) or erroneous program output specified in the testcase (see point 3). A testcase that does not reproduce the problem is useless: if we get different errors or output, then the code apparently does not correspond to the actual code you're having problems with, and consequently analyzing it is pointless. The same is true if the testcase works fine.

A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem.
The code that contains the ArqSequence is located within the ansi.h file. This header file has many inline functions that deal with ansi escape sequences such as changing the cursor position and color output to the screen.

As for reproducing the problem, I was trying to save myself from retyping nearly 700 lines or more of code for all files, thus trying to prevent anyone from getting a "migraine." Unfortunately I am not able to copy and paste the files in their full scope because the editor being used does not allow for it. Sorry to have misspent anyone's time.
Is the operator<< function declared inline?
Directly from the ansi.h file:

//overload the << operator so ostream can use the sequence
std::ostream& operator<<(std::ostream& os, const ArqSequence &aSeq)
{
return aSeq(os);
}

Not sure if that helps, but I did notice that while looking at the instructor's files, he has included the ansi.h file in all of his .cpp files, even his main.cpp file.
I thought maybe it had something to do with how I wrote my makefile and the order in which it compiles the files, but no dice. Basically, what the ansi.h file does is it allows me to type something like:

cout << cursorPosition(2,5) << redBackground << yellow
<< "Hello World";

The ansi.h file basically just returns the ansi/ascii codes for the color changes.

I have used the ansi.h file with other programs with no compilation errors, so I know it is something that I am doing. Maybe by including it twice possibly. Unsure at this point. Thank you for everyone's help to this problem.
Yeah, if you include that in two (or more) different files, it'll end up getting defined in each of those files, so then when the linker comes along to link all of your files together, it'll notice those multiple definitions and spit out an error.

Try changing it to
1
2
3
4
5
//overload the << operator so ostream can use the sequence
inline std::ostream& operator<<(std::ostream& os, const ArqSequence &aSeq)
{
    return aSeq(os);
}
and see if that helps.

Do you get the "multiple definition of ..." error with other functions that are defined in ansi.h (and not declared inline) as well?
Tried changing to inline as advised, no dice. And yes, I do get multiple definition of errors with other functions that are defined in ansi.h. However, they are declared inline. Here are some of the errors I am seeing:

homescreen.o In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ArgSequence const&)':

homescreen.cpp: (.text+0x0): multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >& ArgSequence const&)'
scoreboard.o scoreboard.cpp (.text+0x0): first defined here

homescreen.o In function `cursorUp(int)':
homescreen.cpp: (.text 0x25): multiple definition of `cursorUp(int)'

scoreboard.o:scoreboard.cpp: (.text+0x25): first defined here
Have you tried recompiling everything (that is, running a make clean and then recompiling)?

If it still doesn't work after that, then I suppose that as a last resort, you can try moving all of your function definitions out of ansi.h and into, say, ansi.cpp, and have ansi.h contain only the declarations (no definitions) of the functions.
You'll have to compile ansi.cpp as well, of course.

If you do that, it might be a good idea to wipe the inline off of those functions. (I'm not sure if this is necessary, but whatever...)
I assume that running a make clean would be the same as:

Command to remove .o files
gmake clean

Command to compile and link
gmake

Not sure if I mentioned it, but I am using a putty terminal with the g++ compiler.
> Unfortunately I am not able to copy and paste the files in their full scope
> because the editor being used does not allow for it.
¿why are you using such crappy tools?

Isolate the problem as much as possible

The larger the testcase, the harder it is to analyze. This is especially true when the analyst is not familiar with the program that the testcase was extracted from. Therefore, it is very important that the testcase be as small as possible: every function/variable/class/template/etc that does not directly contribute to the problem should be cut.
of course, it should always reproduce the problem.


Given that you are using several files, you could upload them to github or similar.
Topic archived. No new replies allowed.