简体   繁体   中英

How can i call a operator overloaded member function(or use the operator) from another operator overloading member function in the same class?

I'm writing a code in c++ for handling Complex numbers. I was also practicing operator overloading too. So I overloaded * (multiplication operator), now I want to use the overloaded operator in my overloaded / (division operator), but when I'm using * its showing error. Here is the code:

#include <iostream>
#include <cmath>

using namespace std;

class Imaginary
{
    public:
    //constructors
    Imaginary(double a,double b):x(a),y(b){}
    Imaginary():x(0.0),y(0.0){}

    //setter methods for x and y
    void Setx(double x) { this->x = x; }
    void Sety(double y) { this->y = y; }

    //getter methods for x and y
    double Getx(){return this->x;}
    double Gety(){return this->y;}

    //overloaded operators
    Imaginary operator+(Imaginary&);
    Imaginary operator-(Imaginary&);
    Imaginary operator*(Imaginary&);
    Imaginary operator~();
    Imaginary operator/(Imaginary&);

    void print();
private:
    double x;
    double y;
};

Imaginary Imaginary::operator+(Imaginary &i){
    Imaginary ti;
    ti.Setx(this->x+i.x);
    ti.Sety(this->y+i.y);

    return ti;
}

Imaginary Imaginary::operator-(Imaginary &i){
    Imaginary ti;
    ti.Setx(this->x-i.x);
    ti.Sety(this->y-i.y);
    return ti;
}

Imaginary Imaginary::operator*(Imaginary &i){
Imaginary ti;
ti.Setx((this->x*i.x) - (this->y*i.y));
ti.Sety((this->y*i.x)+(this->x*i.y));
return ti;
}

Imaginary Imaginary::operator~(){
int y;
y = this->y;
this->y = -y;
return *this;
}

Imaginary Imaginary ::operator/(Imaginary &i){
Imaginary numerator,denominator,ti;
//i want to use here the overloaded *(multiplacation) operator
numerator = (*this) * (~i);//showing error
denominator = (*this) * (~i);//showing error
ti.Setx(numerator.Getx()/denominator.Getx());
ti.Sety(numerator.Gety()/denominator.Getx());

return ti;
}

void Imaginary::print(){
cout<<x;
if (y>0)
cout<<"+i"<<y<<endl;
else if (y<0)
cout<<"-i"<<abs(y)<<endl;

}

int main()
{   
Imaginary res;
Imaginary z1(2,3);
Imaginary z2(1,-1);
z1.print();
z2.print();

/*res = z1+z2;
cout<<"Addition:-\n";
res.print();

res = z1-z2;
cout<<"Subtraction:-\n";
res.print()*/

res = z1*z2;
cout<<"Multiplication:-\n";
res.print();

res = z1/z2;
cout<<"Division:-\n";
res.print();

return 0;
}

the error message is following:-

D:\Games\Cheese\main.cpp|66|error: no match for 'operator*' (operand types are 'Imaginary' and 'Imaginary')|

Please anybody show me how to rectify it.

The error isn't the best one in this case. Your operator* is defined as

Imaginary Imaginary::operator*(Imaginary &i)

Which requires the right hand side to be an lvalue. When you do

(*this) * (~i)

in operator/ , (~i) returns Imaginary which is a rvalue. You cannot bind that rvalue to the lavue reference so your overload is not considered and you get a compiler error.

The simplest way to fix this is to take a const & instead like

Imaginary Imaginary::operator*(const Imaginary &i)

Two things

Firstly,

 (*this)*(~i);

is an expression in which the right hand side (result of operator~() ) is a temporary. Your operator*() can only accept a temporary as a reference argument ( Imaginary & )if that argument is declared const .

Also both sides of an operator*() are better specified as const , since - generally speaking - multiplying two values does not change either one (and produces a distinct result). Same goes for other operators (like operator/() ).

The solution is to change the specification of operator*() (member function) to be

Imaginary Imaginary::operator*(const Imaginary &i) const;

Having done that, an alternative way of calling the operator*() would be to replace

numerator = (*this) * (~i);

with

numerator = this->operator*(~i);

or even (since this is happening in a member function) with

numerator = operator*(~i);     //  this-> is implied

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM