I'm having difficulties understanding the following linker error:
1 2 3 4 5 6 7 8 9 10 11 12
g++ -g -Wall mycompmain.C complex.C complex.H -o comp
/tmp/ccQ5RLlr.o(.text+0x110): In function `operator+(doubleconst&, Complex const&)':
/u1/004/syshicht/hw5/complex.H:30: multiple definition of `operator+(double const&, Complex const&)'
/tmp/cc42XlFh.o(.text+0x110):/u1/004/syshicht/hw5/complex.H:30: first defined here
/tmp/ccQ5RLlr.o(.text+0x13e): In function `operator-(doubleconst&, Complex const&)':
/u1/004/syshicht/hw5/complex.H:34: multiple definition of `operator-(double const&, Complex const&)'
/tmp/cc42XlFh.o(.text+0x13e):/u1/004/syshicht/hw5/complex.H:34: first defined here
/tmp/ccQ5RLlr.o(.text+0x1ac): In function `operator*(doubleconst&, Complex const&)':
/u1/004/syshicht/hw5/complex.H:38: multiple definition of `operator*(double const&, Complex const&)'
/tmp/cc42XlFh.o(.text+0x1ac):/u1/004/syshicht/hw5/complex.H:38: first defined here
collect2: ld returned 1 exit status
make: *** [comp] Error 1
These operators are defined in the following file:
The complex.C file doesn't have any of these operators. It only implements the member operators.
What am I doing wrong that causes these errors?
Thanks.
Is this file included in multiple other files? If so that's your problem. I think making them inline may work, but what you should do for global operators is put them in their own CPP files and just have the prototypes in the HPP file.
If you have included this header in both mycompmain.C and complex.C , then you've defined these operators once in each of them. You are only allowed to define them once overall. If you've included it in just one C file, you've still got troubles as you're compiling complex.H into an object as well.
Header guards only stop you including the same header repeatedly in one c file.
Why are you compiling complex.H ? You're supposed to #include your header files in the C files.