-
January 28th, 2012, 05:50 PM
#1
Linker error: Multiple definition of...
I'm having difficulties understanding the following linker error:
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
These operators are defined in the following file:
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
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.
-
January 28th, 2012, 06:04 PM
#2
Re: Linker error: Multiple definition of...
Never mind, problem solved! (With aid from another forum )
Thanks anyway.
-
January 29th, 2012, 06:39 AM
#3
Re: Linker error: Multiple definition of...
Originally Posted by yshicht
Never mind, problem solved! (With aid from another forum )
Thanks anyway.
And could you share the solution?
Victor Nijegorodov
-
February 3rd, 2012, 02:45 PM
#4
Re: Linker error: Multiple definition of...
For noobs that read this and wonder what the solution was, do a search with the text "multiply defined operator c++" and lots of threads will come up. I have run into this myself and usually the solution is either to make the functions in question inline or define the implementations within the impl file (.cc, .cpp, etc.)
The confusion is that the OP is wondering why there is the error since the definition is inside the include guards along with the class. At first glance, one might think that is okay, until the linker indicates otherwise!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|