如何从同一类中的另一个运算符重载成员函数调用运算符重载成员函数(或使用该运算符)? 我正在编写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; }

如何从同一类中的另一个运算符重载成员函数调用运算符重载成员函数(或使用该运算符)? 我正在编写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; },c++,operator-overloading,C++,Operator Overloading,请任何人告诉我如何纠正它。在这种情况下,错误不是最好的。您的运算符*定义为 Imaginary Imaginary::operator*(Imaginary &i) 这要求右手边是左值。当你这样做的时候 (*this) * (~i) 在操作符/中,(~i)返回虚值,该值为右值。您无法将该rvalue绑定到lavue引用,因此不会考虑您的重载,并且会出现编译器错误 解决此问题的最简单方法是使用常量&代替 Imaginary Imaginary::operator*(const Imag

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

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

Imaginary Imaginary::operator*(Imaginary &i)
这要求右手边是左值。当你这样做的时候

(*this) * (~i)
操作符/
中,
(~i)
返回
虚值
,该值为右值。您无法将该rvalue绑定到lavue引用,因此不会考虑您的重载,并且会出现编译器错误

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

Imaginary Imaginary::operator*(const Imaginary &i)
两件事

首先,

 (*this)*(~i);
是一个表达式,其中右侧(运算符的结果)()是临时的。如果将临时变量(
虚&
)声明为
const
,则您的
运算符*()
只能接受该临时变量作为引用参数(
虚&

另外,
运算符*()
的两侧最好指定为
常量,因为一般来说,两个值相乘不会改变其中一个(并产生不同的结果)。其他操作符也是如此(如
operator/()

解决方案是将
操作符*()
(成员函数)的规范更改为

这样做之后,调用
操作符*()
的另一种方法是替换

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

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


运算符也可以是常量。对不起,什么是lvaule和rvalue???@uzumakisaptashi请看这里:实现中的一个(可能)问题是除法更改了操作数
i
,这可能是意外的。因为它是2倍的补码,所以它被改回来了。这就是为什么您应该将参数和函数标记为
const
,在这里您不想更改参数或对象本身(这里除了
操作符
,更改对象本身的每个操作符),所以
..::operatorx(const-virtual&i)const
Imaginary Imaginary::operator*(const Imaginary &i) const;
numerator = (*this) * (~i);
numerator = this->operator*(~i);
numerator = operator*(~i);     //  this-> is implied