簡體   English   中英

如何在同一個類中調用另一個運算符重載成員函數的運算符重載成員函數(或使用運算符)?

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

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

請有人告訴我如何糾正它。

在這種情況下,錯誤不是最好的錯誤。 您的operator*定義為

Imaginary Imaginary::operator*(Imaginary &i)

這要求右手邊是左值。 當你這樣做

(*this) * (~i)

operator/(~i)返回作為右值的Imaginary 您不能將該rvalue綁定到lavue引用,因此不會考慮您的重載並且您會收到編譯器錯誤。

解決這個問題的最簡單方法是采用const &而不是

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

兩件事情

首先,

 (*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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM