在重载 + 和 - 运算符时出错
Getting error while overlloading + and - operator
重载运算符 + 实现以下操作:
- Obj1 + Obj2
- 12 + 目标2
- 目标1 + 10
重载运算符 - 实现以下操作:
- Obj1 - Obj2
- 12 - Obj2
- 目标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);
。
因此,我想说内部运算符重载的工作方式就像使用 dot(.) 运算符调用函数一样。因此Obj1.addInt(5)
将返回一个类型为 Complex 的对象,并将在Obj2
中分配。类似地,Obj1 + 5
也将返回 Complex 类型的对象。
因此,如果您不提供复杂类型的第一个参数,就像您这样做一样Complex Obj4 = 10 + Obj3;
这将扩展类似Obj4= 10.AddInt(Obj4);
.现在10
不是任何对象,那么我们如何使用点运算符调用方法(成员函数)。
但是友元函数的内部工作是不同的。现在,每当我们定义一个友元函数时,对于相同的操作,它都需要两个参数。 这意味着友元函数不使用 dot(.) 运算符调用任何东西,而是接受两个参数并返回一个 Complex 类型的对象。
注意:- 如果两个参数都是复杂类型,那么它工作正常,因为复杂Obj3 = Obj1 + Obj2;
将扩展到复杂Obj3 = Obj1.AddComplex(Obj2);
,Complex Obj3 = Obj2 + Obj1;
将扩展到Complex Obj3 = Obj2.AddComplex(Obj1);
。因此,在这两种情况下,第一个参数都是复杂的。
- 在 myVector 类中重载运算符 + 时出错
- 使用运算符调用 void 函数时出错<set>
- 通过 [] 运算符访问unordered_map时出错
- 在重载 + 和 - 运算符时出错
- 重载运算符时出错<在 sf::Vector 中
- 将运算符<<与隐式转换的非基本数据类型一起使用时出错
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- 令牌之前缺少二进制运算符 "#" 当 #define 在 #if 内时出错
- 在运算符中将联合作为参数传递时出错<<
- 迭代器关系运算符出错(带单独链接和迭代器的自定义哈希表)
- 尝试重载模板类友元<<运算符时出错
- 使用 getline 重载 istream 运算符>> 会出错
- 灵气:用期望运算符替换序列时出错
- 在类外部定义显式运算符 bool() 时出错
- 擦除矢量中的对象时出错.如何将移动作业运算符添加到我的类中
- 过载运算符出错>>
- 在使用C (Microsoft vs)中使用指针和新运算符时出错
- 在C++中复制矢量时,矢量运算符出错=
- 具有std::原子成员变量的类的复制构造函数/赋值运算符出错
- JavaScript到C++的转换:%运算符出错