Linker error: Multiple definition of...

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+(double const&, 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-(double const&, 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*(double const&, 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:
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
#ifndef _COMPLEX
#define _COMPLEX

#include <iostream>
using namespace std;

class Complex {
private:
	double r, i;
public:
	Complex(const double& a = 0, const double& b = 0);
	Complex(const Complex& complexNum);
	Complex operator+(const Complex& complexNum) const;
	Complex operator-(const Complex& complexNum) const;
	Complex operator*(const Complex& complexNum) const;
	Complex operator+(const double& num) const;
	Complex operator-(const double& num) const;
	Complex operator*(const double& num) const;
	Complex& operator+=(const Complex& complexNum);
	Complex& operator-=(const Complex& complexNum);
	Complex& operator*=(const Complex& complexNum);
	Complex& operator+=(const double& num);
	Complex& operator-=(const double& num);
	Complex& operator*=(const double& num);
	bool operator==(const Complex& complexNum) const;
	bool operator!=(const Complex& complexNum) const;
	friend ostream& operator<<(ostream& stream, const Complex& complexNum);
};

Complex operator+(const double& lhs, const Complex& rhs) {
	return rhs + lhs;
}

Complex operator-(const double& lhs, const Complex& rhs) {
	return Complex(-1) * rhs + lhs;
}

Complex operator*(const double& lhs, const Complex& rhs) {
	return rhs * lhs;
}

#endif 


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.
Last edited on
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.
Last edited on
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.

Last edited on
The file complex.H is indeed included in both complex.C and mycompmain.C.

You were right, that's what caused the problem. When I moved the functions to complex.C and only declared them in the header it worked as expected.

Thanks!
Topic archived. No new replies allowed.