为 + 运算符重载 C++
Operator Overloading C++ for + operator
我目前正在学习如何在 C++ 中进行运算符重载,我在网上找到了一些在 运行 以下时有效的代码。
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator+(Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}
然而,当我尝试类似的结构时,我收到以下错误:未找到默认构造函数和 << 我不熟悉的错误。
class Chicken {
private:
int i;
int k;
int s;
int total = 0;
public:
Chicken(int index, int kentucky, int sign) {
i = index;
k = kentucky;
s = sign;
}
Chicken operator+(Chicken obj) {
Chicken Chicky;
Chicky.total = i + obj.i;
return Chicky;
}
};
int main() {
Chicken P(1, 2, 3);
Chicken U(2, 2, 2);
Chicken W = P + U;
cout << W;
}
enter image description here
Below 是更正后的例子。首先,您收到错误是因为在 operator+
中您默认构造了一个 Chicken 类型的对象,但您的 class 没有任何默认构造函数。 解决方法是添加默认构造函数
#include <iostream>
class Chicken{
//needed for cout<<W; to work
friend std::ostream& operator<<(std::ostream& os, const Chicken &rhs);
private:
int i = 0;
int k = 0;
int s = 0;
int total = 0;
public:
//default constructor
Chicken() = default;
//use constructor initializer list instead of assigning inside the body
Chicken(int index, int kentucky, int sign): i(index), k(kentucky), s(sign){
//i = index;
//k = kentucky;
//s = sign;
}
Chicken operator + (Chicken obj){
Chicken Chicky;//this needs/uses default constructor
Chicky.total = i + obj.i;
return Chicky;
}
};
std::ostream& operator<<(std::ostream& os, const Chicken &rhs)
{
os << rhs.i << rhs.k << rhs.s << rhs.total ;
return os;
}
int main(){
Chicken P(1,2,3); //this uses parameterized constructor
Chicken U(2,2,2); //this uses parameterized constructor
Chicken W = P+U; //this uses overloaded operator+
std::cout << W; //this uses oveloaded operator<<
}
其次,您使用了语句 cout<<W;
但没有重载 operator<<
。要使 std::cout<< W;
正常工作,您需要重载 operator<<
,如我在上面的代码中所示。
您可以查看工作示例 here。
第三,您 can/should 使用 构造函数初始化列表 而不是如上所示在构造函数体内赋值。
我目前正在学习如何在 C++ 中进行运算符重载,我在网上找到了一些在 运行 以下时有效的代码。
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator+(Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}
然而,当我尝试类似的结构时,我收到以下错误:未找到默认构造函数和 << 我不熟悉的错误。
class Chicken {
private:
int i;
int k;
int s;
int total = 0;
public:
Chicken(int index, int kentucky, int sign) {
i = index;
k = kentucky;
s = sign;
}
Chicken operator+(Chicken obj) {
Chicken Chicky;
Chicky.total = i + obj.i;
return Chicky;
}
};
int main() {
Chicken P(1, 2, 3);
Chicken U(2, 2, 2);
Chicken W = P + U;
cout << W;
}
enter image description here
Below 是更正后的例子。首先,您收到错误是因为在 operator+
中您默认构造了一个 Chicken 类型的对象,但您的 class 没有任何默认构造函数。 解决方法是添加默认构造函数
#include <iostream>
class Chicken{
//needed for cout<<W; to work
friend std::ostream& operator<<(std::ostream& os, const Chicken &rhs);
private:
int i = 0;
int k = 0;
int s = 0;
int total = 0;
public:
//default constructor
Chicken() = default;
//use constructor initializer list instead of assigning inside the body
Chicken(int index, int kentucky, int sign): i(index), k(kentucky), s(sign){
//i = index;
//k = kentucky;
//s = sign;
}
Chicken operator + (Chicken obj){
Chicken Chicky;//this needs/uses default constructor
Chicky.total = i + obj.i;
return Chicky;
}
};
std::ostream& operator<<(std::ostream& os, const Chicken &rhs)
{
os << rhs.i << rhs.k << rhs.s << rhs.total ;
return os;
}
int main(){
Chicken P(1,2,3); //this uses parameterized constructor
Chicken U(2,2,2); //this uses parameterized constructor
Chicken W = P+U; //this uses overloaded operator+
std::cout << W; //this uses oveloaded operator<<
}
其次,您使用了语句 cout<<W;
但没有重载 operator<<
。要使 std::cout<< W;
正常工作,您需要重载 operator<<
,如我在上面的代码中所示。
您可以查看工作示例 here。
第三,您 can/should 使用 构造函数初始化列表 而不是如上所示在构造函数体内赋值。