如何从同一类中的另一个运算符重载成员函数调用运算符重载成员函数(或使用该运算符)? [英] How can i call a operator overloaded member function(or use the operator) from another operator overloading member function in the same class?

查看:98
本文介绍了如何从同一类中的另一个运算符重载成员函数调用运算符重载成员函数(或使用该运算符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C ++编写代码来处理复数.我也在练习操作符重载.所以我重载了*(乘法运算符),现在我想在重载的/(除法运算符)中使用重载运算符,但是当我使用*时,它显示错误.这是代码:

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;
}

错误消息如下:-

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.

推荐答案

在这种情况下,错误不是最好的错误.您的operator*定义为

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)

operator/中的

(~i)返回作为右值的Imaginary.您无法将该右值绑定到lavue参考,因此不会考虑您的重载,并且会出现编译器错误.

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.

解决此问题的最简单方法是采用const &代替

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

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

这篇关于如何从同一类中的另一个运算符重载成员函数调用运算符重载成员函数(或使用该运算符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆