C++ 错误:“表达式必须具有整数或无作用域的枚举类型”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39602344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: "expression must have integral or unscoped enum type"
提问by Victor Martins
So guys I'm new I tried looking up a solution however, I didn't really understand it that well, I'm writing shorthand to understand how they can be replaced with other pieces of code, so I ran across the modulus to which I added however it gives a "expression must have integral or unscoped enum type".
所以伙计们,我是新手,我尝试寻找一个解决方案,但是,我并没有真正理解它,我正在编写速记以了解如何用其他代码段替换它们,所以我遇到了模数但是我补充说,它给出了 “表达式必须具有整数或无作用域的枚举类型”。
I don't know enum type is exactly, they code doesn't run?
我不知道 enum 类型到底是什么,他们的代码不运行?
#include<iostream>
#include<string>
using namespace std;
int main() {
double b, x, y, z, a, c;
c, b, x, y, z, a, c = 100;
x += 5;
y -= 2;
z *= 10;
a /= b;
c %= 3; // "c" seems to be giving out that error?
cout << b << x << y << z << a << c;
return 0;
}
the problem here is that "c" gives the "expression must have integral or unscoped enum type"error.
这里的问题是“c”给出了“表达式必须具有整数或无作用域枚举类型”错误。
I know what the modulus does, it gives the remainder of the division between 2 numbers, however I'm stumped in this case because should it gives a remainder? Is it syntactically wrong?
我知道模数的作用,它给出了两个数字之间的余数,但是在这种情况下我很难过,因为它应该给出余数吗?它在语法上是错误的吗?
回答by gsamaras
c
is double, thus you cannot use the modulo operator %
.
c
是 double,因此您不能使用模运算符%
。
Use fmod()instead.
请改用fmod()。
So change this:
所以改变这个:
c %= 3
to this:
对此:
c = fmod(c, 3);
As Slava mentioned, you could have used an int
instead, like this:
正如 Slava 所提到的,您可以使用 anint
来代替,如下所示:
int c = 5; // for example
c %= 3
which wouldn't require the use of fmod()
. It's important to understand that the modulo operator %
works with int
s.
这不需要使用fmod()
. 了解模运算符%
与int
s一起使用很重要。
As π?ντα ρ?ι mentioned, there is also this: Can't use modulus on doubles?
正如 π?ντα ρ?ι 所提到的,还有这个:不能在双打上使用模数吗?
As a side note Victor, you have so many variables, but most of them are unused and or uninitialized. Did you compile with all the warnings enabled? Here is what I get when compiling your original code (with the line that generates the error commented out):
作为旁注 Victor,您有很多变量,但其中大部分未使用或未初始化。您是否在启用所有警告的情况下进行编译?这是编译原始代码时得到的结果(注释掉生成错误的行):
C02QT2UBFVH6-lm:~ gsamaras$ g++ -Wall main.cpp
main.cpp:9:5: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:8: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:11: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:14: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:17: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:9:20: warning: expression result unused [-Wunused-value]
c, b, x, y, z, a, c = 100;
^
main.cpp:10:5: warning: variable 'x' is uninitialized when used here [-Wuninitialized]
x += 5;
^
main.cpp:7:16: note: initialize the variable 'x' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
main.cpp:11:5: warning: variable 'y' is uninitialized when used here [-Wuninitialized]
y -= 2;
^
main.cpp:7:19: note: initialize the variable 'y' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
main.cpp:12:5: warning: variable 'z' is uninitialized when used here [-Wuninitialized]
z *= 10;
^
main.cpp:7:22: note: initialize the variable 'z' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
main.cpp:13:5: warning: variable 'a' is uninitialized when used here [-Wuninitialized]
a /= b;
^
main.cpp:7:25: note: initialize the variable 'a' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
main.cpp:13:10: warning: variable 'b' is uninitialized when used here [-Wuninitialized]
a /= b;
^
main.cpp:7:13: note: initialize the variable 'b' to silence this warning
double b, x, y, z, a, c;
^
= 0.0
11 warnings generated.