我在我的代码中使用了枚举,它崩溃了
I have used enum in my code and it is crashing
我在下面的代码中使用了枚举并使用了运算符重载,但它崩溃了。谁能解释一下为什么?
#include<iostream>
using namespace std;
enum E{M, T= 3, W, Th, F, Sa, Su};
E operator+(const E &a, const E &b){
unsigned int ea = a, eb = b;
unsigned int ec = (a+b)%7;
return E(ec);
}
int main(){
E a = M, b = F;
cout<<a<<endl;
cout<<b<<endl;
E day = a+b;
//cout<<int(day)<<endl;
return 0;
}
E operator+(const E &a, const E &b){
这为 class E
.
的两个实例定义了 +
运算符
unsigned int ec = (a+b)%7;
a
和 b
都是 class E
的实例,因此 +
操作将通过调用您的 operator+
来完成过载。
除了这首先是您的 operator+
过载。因此,这最终将永久地再次调用 operator+
,导致无限递归和崩溃。
这就是显示的代码崩溃的原因。
我在下面的代码中使用了枚举并使用了运算符重载,但它崩溃了。谁能解释一下为什么?
#include<iostream>
using namespace std;
enum E{M, T= 3, W, Th, F, Sa, Su};
E operator+(const E &a, const E &b){
unsigned int ea = a, eb = b;
unsigned int ec = (a+b)%7;
return E(ec);
}
int main(){
E a = M, b = F;
cout<<a<<endl;
cout<<b<<endl;
E day = a+b;
//cout<<int(day)<<endl;
return 0;
}
E operator+(const E &a, const E &b){
这为 class E
.
+
运算符
unsigned int ec = (a+b)%7;
a
和 b
都是 class E
的实例,因此 +
操作将通过调用您的 operator+
来完成过载。
除了这首先是您的 operator+
过载。因此,这最终将永久地再次调用 operator+
,导致无限递归和崩溃。
这就是显示的代码崩溃的原因。