简体   繁体   English

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

[英]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. 我正在用c ++编写代码来处理复数。 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 您的operator*定义为

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. operator/(~i)返回作为右值的Imaginary You cannot bind that rvalue to the lavue reference so your overload is not considered and you get a compiler error. 您不能将该rvalue绑定到lavue引用,因此不会考虑您的重载并且您会收到编译器错误。

The simplest way to fix this is to take a const & instead like 解决这个问题的最简单方法是采用const &而不是

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. 是右侧( operator~() )是临时的表达式。 Your operator*() can only accept a temporary as a reference argument ( Imaginary & )if that argument is declared const . 如果该参数声明为const ,则operator*()只能接受临时作为引用参数( Imaginary & )。

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). operator*()两边最好指定为const ,因为 - 一般来说 - 乘以两个值不会改变任何一个(并产生不同的结果)。 Same goes for other operators (like operator/() ). 其他运算符也是如此(例如operator/() )。

The solution is to change the specification of operator*() (member function) to be 解决方案是将operator*() (成员函数)的规范更改为

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

Having done that, an alternative way of calling the operator*() would be to replace 完成后,调用operator*()的另一种方法是替换

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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