c++ - 重载 + 和 - 运算符时出错
问题描述
重载运算符 + 以实现以下操作:
- 对象 1 + 对象 2
- 12 + 对象2
- 对象 1 + 10
重载运算符 - 实现以下操作:
- 对象 1 - 对象 2
- 12 - 对象2
- 对象 1 - 10
主要看重载:Obj1 - 10, 12 - Obj2, 12 + Obj2, Obj1 + 10 案例我想重载运算符 + 和 - 以便处理以上所有操作。如何处理这些操作/案例?在这里,我面临第二种情况的问题。我正在考虑只为 + 和 - 编写一个函数来处理这些情况。
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
Complex operator + (Complex const &obj) {
Complex res;
res.real = this->real + obj.real;
res.imag = this->imag + obj.imag;
return res;
}
Complex operator + (int i) {
Complex res;
res.real = this->real + i;
res.imag = this->imag ;
return res;
}
Complex operator - (Complex const &obj) {
Complex res;
res.real = this->real - obj.real;
res.imag = this->imag - obj.imag;
return res;
}
Complex operator - (int i) {
Complex res;
res.real = this->real - i;
res.imag = this->imag ;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main()
{
Complex Obj1(10, 5), Obj2(2, 4);
Complex Obj3 = Obj1 + Obj2;
Complex Obj4 = 10 + Obj3;
Complex Obj5 = Obj4 + 15;
cout<<" + operation:"<<endl;
Obj3.print();
Obj4.print();
Obj5.print();
Complex Obj6 = Obj1 - Obj2;
Complex Obj7 = 10 - Obj3;
Complex Obj8 = Obj4 - 15;
cout<<" - operation:"<<endl;
Obj6.print();
Obj7.print();
Obj8.print();
}
预期输出:
+ operation:
12 + i9
22 + i9
37 + i9
- operation:
8 + i
2 + i9
7 + i9
出现以下错误:
error: no match for 'operator+' (operand types are 'int' and 'Complex')
Complex Obj4 = 10 + Obj3;
解决方案
不Complex Obj4 = 10 + Obj3;
应该是这样 应该是这样Complex Obj4 = Obj3 + 10;
根据您定义的重载运算符的函数,第一个参数应该是复杂类型而不是 int 类型。
像这样做:-
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
Complex operator + (Complex const &obj) {
Complex res;
res.real = this->real + obj.real;
res.imag = this->imag + obj.imag;
return res;
}
friend Complex operator + (int i, Complex const &obj) {
Complex res;
res.real = obj.real + i;
res.imag = obj.imag ;
return res;
}
Complex operator - (Complex const &obj) {
Complex res;
res.real = this->real - obj.real;
res.imag = this->imag - obj.imag;
return res;
}
friend Complex operator - (int i, Complex const &obj) {
Complex res;
res.real = obj.real - i;
res.imag = obj.imag ;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main()
{
Complex Obj1(10, 5), Obj2(2, 4);
Complex Obj3 = Obj1 + Obj2;
Complex Obj4 = 10 + Obj3;
Complex Obj5 = Obj4 + 15;
cout<<" + operation:"<<endl;
Obj3.print();
Obj4.print();
Obj5.print();
Complex Obj6 = Obj1 - Obj2;
Complex Obj7 = 10 - Obj3;
Complex Obj8 = Obj4 - 15;
cout<<" - operation:"<<endl;
Obj6.print();
Obj7.print();
Obj8.print();
}
如果您想为每个(+ 和 -)编写一个函数,请考虑使用模板。
正如我已经告诉过你的,第一个参数应该是复杂类型的,但我想我应该解释一下为什么?
下面是两个程序尝试仔细理解它们。
第一个程序:
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
Complex operator +(int i) {
Complex res;
res.real = this->real + i;
res.imag = this->imag;
return res;
}
void printComplex(){
cout<<"Real="<<this->real<<" Imaginary="<<this->imag;
}
};
int main()
{
Complex Obj1(10, 5);
Complex Obj2;
Obj2=Obj1 + 5;
Obj2.printComplex();
return 0;
}
第二个程序:
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}
Complex addInt(int i) {
Complex res;
res.real = this->real + i;
res.imag = this->imag;
return res;
}
void printComplex(){
cout<<"Real="<<this->real<<" Imaginary="<<this->imag;
}
};
int main()
{
Complex Obj1(10, 5);
Complex Obj2;
Obj2=Obj1.addInt(5);
Obj2.printComplex();
return 0;
}
分别考虑语句Obj2=Obj1 + 5;
和Obj2=Obj1.addInt(5);
在第一个程序和第二个程序中。
所以从这里我想说的是,内部运算符重载就像使用点(。)运算符调用函数一样。所以Obj1.addInt(5)
将返回一个 Complex 类型的对象,并将在Obj2
. 同样Obj1 + 5
也会返回一个 Complex 类型的对象。
因此,如果您不提供 Complex 类型的第一个参数,就像您所做的那样Complex Obj4 = 10 + Obj3;
,它将扩展类似Obj4= 10.AddInt(Obj4);
. 现在10
不是任何对象,所以我们如何使用点运算符调用方法(成员函数)。
但是友元函数的内部工作是不同的。现在,每当我们定义一个友元函数时,对于同一个操作,它都需要两个参数。这意味着朋友函数不使用点(。)运算符调用任何东西,而是接受两个参数并返回一个复杂类型的对象。
注意:- 如果两个参数都是 Complex 类型,那么它可以正常工作,因为 ComplexObj3 = Obj1 + Obj2;
将扩展为 ComplexObj3 = Obj1.AddComplex(Obj2);
并将Complex Obj3 = Obj2 + Obj1;
扩展为Complex Obj3 = Obj2.AddComplex(Obj1);
. 所以在这两种情况下,第一个参数都是 Complex。
推荐阅读
- google-apps-script - 如何获取线程中的最后一条消息?
- java - 为什么之前的sql语句又被执行了?
- c# - WPF 事件到父子 DataGrid 中的命令绑定无法绑定子 DataGrid 事件
- python - 如何使用本地库作为 pip 的要求
- python-3.x - 如何过滤此类数据?
- javascript - 抛出错误 RangeError:将自定义日期设置为 react-datepicker 的 DatePicker 时时间值无效
- javascript - 从nodejs中的异步查询中获取结果
- c# - 无法通过 c# 使用 SAS 访问 Azure 存储表。在邮递员和浏览器中工作
- java - 策略游戏地图加载算法中的性能相关问题(Java,lwjgl)
- sql-server - SQLSERVER FOR XML PATH - 将属性名称视为变量