如何用class的参数重载运算符+?
How to overload operator + with the parameters of a class?
我正在学习 C++ 中的重载运算符,我已经完成了关于两个虚数之和的代码,由实部和虚部组成。
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r, int i) {
real = r;
imag = i;
}
Complex operator + (Complex const &num1, Complex const &num2) {
Complex res;
res.real = num1.real + num2.real;
res.imag = num1.imag + num2.imag;
return res;
}
void print() {
cout << real << " + i" << imag << endl;
}
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}
应该是出了什么问题,因为它显示了很多错误、注释和警告:(
error: ‘Complex Complex::operator+(const Complex&, const Complex&)’ must take either zero or one argument
error: no match for ‘operator+’ (operand types are ‘Complex’ and ‘Complex’)
note: ‘Complex’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
您定义 operator+
的方式很好,期望它需要是一个 friend
函数。另外,请注意 Complex res;
不会编译,因为您没有默认构造函数。
您可以这样定义函数:
friend Complex operator + (Complex const &num1, Complex const &num2) {
return {num1.real + num2.real, num1.imag + num2.imag};
}
这是 demo。
另请注意,我修复了两次添加 num1
的虚部的错误。
A binary (2 parameter) operator 不能是 class 成员,它需要是一个独立的函数:
class Complex {
...
public:
...
friend Complex operator + (Complex const &lhs, Complex const &rhs);
...
};
Complex operator + (Complex const &lhs, Complex const &rhs) {
return Complex(
lhs.real + rhs.real,
lhs.imag + rhs.imag
);
}
也可以内联:
class Complex {
...
public:
...
friend Complex operator + (Complex const &lhs, Complex const &rhs) {
return Complex(
lhs.real + rhs.real,
lhs.imag + rhs.imag
);
}
...
};
因此,像 c1 + c2
这样的语句被处理为 operator+(c1, c2)
。
A unary (1 parameter) operator, 另一方面,必须是作用于 this
左侧的 class 成员值:
class Complex {
...
public:
...
Complex operator + (Complex const &rhs) const {
return Complex(
real + rhs.real,
imag + rhs.imag
);
}
...
};
然后像 c1 + c2
这样的语句被处理为 c1.operator+(c2)
。
我正在学习 C++ 中的重载运算符,我已经完成了关于两个虚数之和的代码,由实部和虚部组成。
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r, int i) {
real = r;
imag = i;
}
Complex operator + (Complex const &num1, Complex const &num2) {
Complex res;
res.real = num1.real + num2.real;
res.imag = num1.imag + num2.imag;
return res;
}
void print() {
cout << real << " + i" << imag << endl;
}
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}
应该是出了什么问题,因为它显示了很多错误、注释和警告:(
error: ‘Complex Complex::operator+(const Complex&, const Complex&)’ must take either zero or one argument
error: no match for ‘operator+’ (operand types are ‘Complex’ and ‘Complex’)
note: ‘Complex’ is not derived from ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
您定义 operator+
的方式很好,期望它需要是一个 friend
函数。另外,请注意 Complex res;
不会编译,因为您没有默认构造函数。
您可以这样定义函数:
friend Complex operator + (Complex const &num1, Complex const &num2) {
return {num1.real + num2.real, num1.imag + num2.imag};
}
这是 demo。
另请注意,我修复了两次添加 num1
的虚部的错误。
A binary (2 parameter) operator 不能是 class 成员,它需要是一个独立的函数:
class Complex {
...
public:
...
friend Complex operator + (Complex const &lhs, Complex const &rhs);
...
};
Complex operator + (Complex const &lhs, Complex const &rhs) {
return Complex(
lhs.real + rhs.real,
lhs.imag + rhs.imag
);
}
也可以内联:
class Complex {
...
public:
...
friend Complex operator + (Complex const &lhs, Complex const &rhs) {
return Complex(
lhs.real + rhs.real,
lhs.imag + rhs.imag
);
}
...
};
因此,像 c1 + c2
这样的语句被处理为 operator+(c1, c2)
。
A unary (1 parameter) operator, 另一方面,必须是作用于 this
左侧的 class 成员值:
class Complex {
...
public:
...
Complex operator + (Complex const &rhs) const {
return Complex(
real + rhs.real,
imag + rhs.imag
);
}
...
};
然后像 c1 + c2
这样的语句被处理为 c1.operator+(c2)
。