-1

I am getting error while trying to compile the following code. I am beginner. Please help me figuring it out. I am trying to overload the operators _,+,/,*. It shows error while compiling. Should I use "friend" to solve this?

#include<stdio.h>

class complex{
private:
    double real; //real part of complex
    double imag; // imaginary part of complex
public:
    complex(double r=0., double i=0.):real(r),imag(i)
    {
    } // constructor with initialization
    complex(const complex&c):real(c.real),imag(c.imag)
    {
    } // copy constructor with initialization
    ~complex()
    {
    } // destructor
    double re() const
    {
        return real;
    } // read real part
    double im() const
    {
        return imag;
    } // read imaginary part
    const complex& operator=(const complex&c)
    {
        real=c.real;
        imag=c.imag;
        return *this;
    } //assignment operator
    const complex& operator+=(const complex&c)
    {
        real += c.real;
        imag += c.imag;
        return *this;
    } // addition of current complex
    const complex& operator-=(const complex&c)
    {
        real -= c.real;
        imag -= c.imag;
        return *this;
    } // subtract from current complex
    const complex& operator*=(const complex&c)
    {
        double keepreal = real;
        real = real*c.real-imag*c.imag;
        imag = keepreal*c.imag+imag*c.real;
        return *this;
    } // multiply current complex with a complex
    const complex& operator/=(double d)
    {
        real /= d;
        imag /= d;
        return *this;
    } // divide current complex with real
    void print(const complex&c)
    {
        printf("(%f,%f)\n",c.re(),c.im() );
    } // printing complex number
    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();
    } // absolute value of complex
    const complex& operator/=(const complex&c)
    {
        return *this *= (!c)/=abs2(c);
    } // divide the current complex by a complex
    const complex operator-(const complex& c)
    {
        return complex(-c.re(),-c.im());
    } // negative of complex number
    const complex operator-(const complex& c,const complex& d)
    {
        return complex(c.re()-d.re(),c.im()-d.im());
    } // difference between complex numbers
    const complex operator+(const complex& c,const complex& d)
    {
        return complex(c.re()+d.re(),c.im()+d.im());
    } // addition of complex numbers
    const complex operator*(const complex& c,const complex& d)
    {
        return complex(c)*=d;
    } // multiplication of complex numbers
    const complex operator/(const complex& c,const complex& d)
    {
        return complex(c)/=d;
    } // division of complex numbers
};

int main(){
    complex c(1.,0.),d(3.,4.);
    print(c-d);
    print(c/d);
    return 0;
}

the output I am getting is:

complex_nums.cpp:76:59: error: ‘const complex complex::operator-(const complex&, const complex&)’ must take either zero or one argument
  const complex operator-(const complex& c,const complex& d)
                                                           ^
complex_nums.cpp:80:59: error: ‘const complex complex::operator+(const complex&, const complex&)’ must take either zero or one argument
  const complex operator+(const complex& c,const complex& d)
                                                           ^
complex_nums.cpp:84:59: error: ‘const complex complex::operator*(const complex&, const complex&)’ must take either zero or one argument
  const complex operator*(const complex& c,const complex& d)
                                                           ^
complex_nums.cpp:88:59: error: ‘const complex complex::operator/(const complex&, const complex&)’ must take exactly one argument
  const complex operator/(const complex& c,const complex& d)
                                                           ^
complex_nums.cpp: In function ‘int main()’:
complex_nums.cpp:96:11: error: ‘print’ was not declared in this scope
  print(c-d);
           ^
complex_nums.cpp:97:9: error: no match for ‘operator/’ (operand types are ‘complex’ and ‘complex’)
  print(c/d);
  • 2
    The compiler is exactly telling you that to do. – Hayt Sep 02 '16 at 14:34
  • 2
    What do you mean by `print()`? This code does not compile with a C++ compiler. Please provide a [MCVE] – Khalil Khalaf Sep 02 '16 at 14:35
  • But those member functions need two arguments. – Vishnu Pradeesh Sep 02 '16 at 14:36
  • I have defined a print() function as member function to display the complex number. – Vishnu Pradeesh Sep 02 '16 at 14:37
  • They don't need 2 arguments. You are already inside one object (the "this" class) and you get the other one as argument. – Hayt Sep 02 '16 at 14:39
  • Side note -- *const complex operator-(const complex& c,const complex& d)* -- Why is this and similar two argument functions non-static member functions? You don't use `this` in any way inside of these functions, thus they shouldn't be non-static class members. – PaulMcKenzie Sep 02 '16 at 14:43

3 Answers3

2

All your operators (+,-,*,/) need exactly one or no arguments, unless they are friend functions. Mark all the operator functions as friend, and the code should work. Otherwise, eliminate 1 parameter from each of the operators, and instead of that use the current instance (this). Example for + operator with parameter removed:

const complex operator+(const complex& c)
{
    return complex(c.re()+re(),c.im()+im());
} 

Reference of friend arithmetic operators: http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators-using-friend-functions/

Next Error

What are you doing with your print() function? It should be in global scope, because it takes a complex as parameter. Move print() out of the class into global scope, like this. If you still want to keep a print() for the object itself, do so, but the one for your class should look like below:

class complex
{
    void print(const complex&c)
    {
        printf("(%f,%f)\n",re(),im() );
    } // printing complex number
};
void print(const complex&c)
{
    printf("(%f,%f)\n",c.re(),c.im() );
} // printing complex number
Community
  • 1
  • 1
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
1

Binary operators are either free functions with 2 arguments (preferred) or member functions with one argument (not so good).

You have defined member functions with 2 arguments.

Turning them into free functions by adding friend is one way.

Another is to make them free functions, defined outside the class but written in terms of member functions (as you have done anyway):

struct complex
{
 /* ... */ 
};
// class definition ends

//free function
inline complex operator/(const complex& c,const complex& d)
{
    return complex(c)/=d;
} // division of complex numbers

Note, this function can be improved:

inline complex operator/(complex c,const complex& d)
{
    c /= d;
    return c;
}

Also, unary operators should return complex&, not const complex&. The thing you're returning is mutable, because you just mutated it.

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Hijacking your answer to post an example of using `friend` to accomplish the same thing: [demo](http://coliru.stacked-crooked.com/a/0ea426c2b7524c4a) – AndyG Sep 02 '16 at 14:43
  • Also he needs `HisObject.print()` in `main()` and not just `print()` – Khalil Khalaf Sep 02 '16 at 14:43
0

For the errors with the operators must take either zero or one argument, this is because you are implementing the non-member function as a member funciton. These should be free functions to work properly, or should only take one argument and use this as the other.

See this question for clarification:

Operator overloading : member function vs. non-member function?

For the error with print, I guess you are trying to call the print member function. To do that you need to do e.g. the following:

complex(c-d).print();
Community
  • 1
  • 1
js441
  • 1,134
  • 8
  • 16