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

时间:2017-10-03 16:57:20

标签: c++ operator-overloading

我正在用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')|

请有人告诉我如何纠正它。

2 个答案:

答案 0 :(得分:2)

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

Imaginary Imaginary::operator*(Imaginary &i)

这要求右手边是左值。当你这样做

(*this) * (~i)
operator/中的

(~i)返回Imaginary,这是一个右值。您不能将该右值绑定到lavue引用,因此不会考虑您的重载并且您会收到编译器错误。

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

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

答案 1 :(得分:0)

两件事

首先,

<build>
    <plugins>
        <plugin>
            <groupId>com.ibm.mfp</groupId>
            <artifactId>adapter-maven-plugin</artifactId>
            <extensions>true</extensions>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <excludes><exclude>**/*.java</exclude></excludes>
        </resource>
    </resources>
</build>

是右侧( (*this)*(~i); 的结果)是临时的表达式。如果该参数声明为operator~(),则operator*()只能接受临时参数作为参考参数(Imaginary &)。

const的两边都更好地指定为operator*(),因为 - 一般来说 - 乘以两个值不会改变任何一个(并产生不同的结果)。其他运营商也是如此(例如const)。

解决方案是将operator/()(成员函数)的规范更改为

operator*()

完成此操作后,调用Imaginary Imaginary::operator*(const Imaginary &i) const; 的另一种方法是替换

operator*()

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

或甚至(因为这是在成员函数中发生的)

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