I'm having difficulties understanding the following linker error:
These operators are defined in the following file:Code: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
The complex.C file doesn't have any of these operators. It only implements the member operators.Code:#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
What am I doing wrong that causes these errors?
Thanks.




Reply With Quote