Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5952095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:37:56+00:00 2026-05-22T17:37:56+00:00

#include<iostream> using namespace std; class complex { double real; double image; public: complex(double r=0,double

  • 0
#include<iostream>
using namespace std;

class complex {
    double real;
    double image;
public:
    complex(double r=0,double i=0) : real(r), image(i) { };
    complex(const complex& c) : real(c.real), image(c.image) { };
    ~complex(){};
    double re() const {
        return real;
    };
    double im() const{
        return image;
    };
    const complex& operator =(const complex&c)
    {
        real = c.real;
        image = c.image;
        return *this;
    };
    const complex& operator +=(const complex&c)
    {
        real += c.real;
        image += c.image;
        return *this;
    };
    const complex& operator -=(const complex&c)
    {
        real -= c.real;
        image -= c.image;
        return *this;
    };
    const complex& operator *=(const complex&c)
    {
        double keepreal=real;
        real = real * c.real - image * c.image;
        image = keepreal * c.image + image * c.real;
        return *this;
    };
    const complex& operator /=(double d)
    {
        real/=d;
        image/=d;
        return *this;
    };

    friend complex operator !(const complex &c)
    {
        return complex(c.re(),-c.im());
    };
    friend double abs2(const complex& c)
    {
        return (c.re() * c.re() + c.im() * c.im());
    };
    const complex& operator /=(const complex&c)
    {   
        return *this *= (!c) /= abs2(c);
    };
    const complex operator +(const complex& c, const complex& d)
    {
        return complex(c.re() + d.re(), c.im() + d.im());
    };
    const complex operator -(const complex& c, const complex& d)
    {
        return complex(c.re() - d.re(), c.im() - d.im());
    };
    const complex operator -(const complex&c)
    {
        return complex(-c.re(), -c.im());
    };
    const complex operator /(const complex& c,const complex& d)
    {
        return complex(c) /= d;
    };
};

int main() {
    complex c = 1., d(3.,4.);

    return 0;
}

OUTPUT:

Line 62: error: ‘const complex
complex::operator+(const complex&,
const complex&)’ must take either
zero or one argument
compilation
terminated due to -Wfatal-errors.

please help: http://codepad.org/cOOMmqw1

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:37:57+00:00Added an answer on May 22, 2026 at 5:37 pm

    There are two alternatives to overload the binary + operator, as a free function or as a member function. As a member function the signature is Type operator+( Type const & ) const (const is optional there, but I am assuming that a+b does not modify a, which seems a fair assumption).

    The alternative approach is using a free function that takes two objects and returns the sum. There are different signatures for this, but the most widely accepted would be: Type operator+( Type lhs, Type const & rhs ) (note that the first argument is by value), where the implementation internally modifies and returns lhs. In particular a common approach to arithmetic operator overloading is implementing operator+= as a member function, and then implementing a free function operator+ in terms of the former:

    struct Type {
       // ...
       Type& operator+=( Type const & );
    };
    Type operator+( Type lhs, Type const & rhs ) {
       return lhs+=rhs;
    }
    

    This way, you don’t need to grant friendship to the free function. In some cases, in particular with templates, it is sometimes recommended to define the operator inside the class definition, and in that case you will have to make it a friend (not for access, but for syntactic reasons:

    struct Type {
       // ...
       Type& operator+=( Type const & );
       // Still a free function:
       friend Type operator+( Type lhs, Type const & rhs ) {
          return lhs+=rhs;
       }
    };
    

    As of the reasons to use this pattern… Implementing operator+= first and then operator+ on top of it provides the two separate operations for little extra cost (operator+ is a one-liner function!). Implementing it as a member function (operator+= could also be a free function) makes it similar to operator= (must be member) and avoids the need for friendship.

    The reason for implementing operator+ as a free function are related to type symmetry of the operation. Addition is commutative (a+b == b+a), and you would expect the same while using your type. You have provided an implicit constructor (your complex type can be implicitly converted from an int due to the constructor complex( double r = 0, double i = 0 ), and that allows the compiler to use those conversions if a function call does not perfectly match an overload.

    If operator+ is implemented as a member function, the compiler is only allowed to consider that overload when the first argument is a complex, and will implicitly convert the other argument, allowing you to type complex(0,0)+5. The problem is that if you reverse the order 5+complex(0,0) the compiler cannot convert 5to a complex and then use the member operator+. On the other hand, if you provide it as a free function, the compiler will detect that one of the two arguments matches in both cases, and will try and succeeds converting the other argument. The net result is that by using a free function you are allowing with a single implementation all these three additions: complex+complex, complex+double, double+complex (and additionally for all integral and floating point types, since they can be converted to double

    Having operator+ take the first argument by value means that the compiler can under some circumstances elide a copy: a + b + c binds as (a+b) + c, the temporary result of the first operation can be used directly as argument to the second call, without extra copying.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; class Array { friend ostream &operator<<( ostream &, const
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <iostream> #include <vector> using namespace std; class base { int x; public: base(int
I'm trying to compile such code: #include <iostream> using namespace std; class CPosition {
#include <iostream> using namespace std; int main() { double u = 0; double w
Complex operator*(double m, const Complex & c) { return c * m; } *On
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
code: #include<iostream> using namespace std; template<class T, int N> class point { T coordinate[N];
#include <iostream> #include <string> using namespace std; class BookData { string Title; int Qty;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.