CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2012
    Posts
    9

    Question 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.

  2. #2
    Join Date
    Jan 2012
    Posts
    9

    Re: Linker error: Multiple definition of...

    Never mind, problem solved! (With aid from another forum )
    Thanks anyway.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Linker error: Multiple definition of...

    Quote Originally Posted by yshicht View Post
    Never mind, problem solved! (With aid from another forum )
    Thanks anyway.
    And could you share the solution?
    Victor Nijegorodov

  4. #4
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb 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
  •  





Click Here to Expand Forum to Full Width

Featured