如何在同一类中的另一个运算符重载成员函数中调用运算符重载成员函数(或使用运算符)?

3

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

#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')|

请问有人能告诉我如何纠正这个问题吗?

你的实现可能存在一个问题,即除法会改变操作数“i”,这可能是无意的。由于它是二进制补码,因此会被改回来。这就是为什么在不打算更改参数或对象本身的情况下(这里除了更改对象本身的“operator~”运算符之外的每个运算符),应将参数和函数标记为“const”的原因,因此... :: operatorx(const Imaginary &i) const - LeBlue
2个回答

2

在这种情况下,该错误并不是最好的。你的operator*被定义为:

Imaginary Imaginary::operator*(Imaginary &i)

需要右侧的值为左值。当你这样做时

(*this) * (~i)

在`operator/`中,`(~i)`返回的是一个右值`Imaginary`。你不能将该右值绑定到左值引用上,所以你的重载不被考虑并且会收到编译器错误。
最简单的方法是改为使用`const &`,像这样:
Imaginary Imaginary::operator*(const Imaginary &i)

运算符也可以是 const - François Andrieux
请问,lvalue 和 rvalue 是什么? - Saptarshi Sahoo
@UzumakiSaptarshi 点击这里:https://dev59.com/QnA65IYBdhLWcg3w1SfU - François Andrieux

0

两件事情

首先,

 (*this)*(~i);

是一个表达式,在该表达式中右侧(operator~()的结果)是一个临时对象。如果该临时对象声明为const,则您的operator*()只能接受临时对象作为引用参数(Imaginary &)。

此外,operator*()两侧最好都进行指定为const,通常来讲,乘以两个值不会改变任何一个值(而是生成一个独立的结果)。其他操作符也是如此(例如operator/())。

解决方案是更改operator*()(成员函数)的规范,使其成为

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

完成这个之后,调用operator*()的另一种替代方式是替换。
numerator = (*this) * (~i);

使用

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

或者(因为这是在成员函数中发生的)使用

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接