Error: "expression must have integral or unscoped enum type"
Error: "expression must have integral or unscoped enum type"
伙计们,我是新手,我试着寻找一个解决方案,但是,我并没有真正理解它,我正在写 shorthand 来了解如何将它们替换为其他代码片段,所以我 运行 跨越我添加的模数但是它给出了 "expression must have integral or unscoped enum type".
我不知道枚举类型到底是什么,他们的代码不是 运行?
#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;
}
这里的问题是 "c" 给出了 "expression must have integral or unscoped enum type" 错误。
我知道模数的作用,它给出了 2 个数字之间的除法余数,但是在这种情况下我很困惑,因为它应该给出余数吗?语法错误吗?
c
是双精度数,因此不能使用模运算符 %
.
改用fmod()。
所以改变这个:
c %= 3
对此:
c = fmod(c, 3);
正如 Slava 提到的,您可以使用 int
代替,如下所示:
int c = 5; // for example
c %= 3
不需要使用 fmod()
。重要的是要了解模运算符 %
与 int
s.
一起使用
正如πìντα ρέι 提到的,还有这个:Can't use modulus on doubles?
顺便说一句,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.
伙计们,我是新手,我试着寻找一个解决方案,但是,我并没有真正理解它,我正在写 shorthand 来了解如何将它们替换为其他代码片段,所以我 运行 跨越我添加的模数但是它给出了 "expression must have integral or unscoped enum type".
我不知道枚举类型到底是什么,他们的代码不是 运行?
#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;
}
这里的问题是 "c" 给出了 "expression must have integral or unscoped enum type" 错误。
我知道模数的作用,它给出了 2 个数字之间的除法余数,但是在这种情况下我很困惑,因为它应该给出余数吗?语法错误吗?
c
是双精度数,因此不能使用模运算符 %
.
改用fmod()。
所以改变这个:
c %= 3
对此:
c = fmod(c, 3);
正如 Slava 提到的,您可以使用 int
代替,如下所示:
int c = 5; // for example
c %= 3
不需要使用 fmod()
。重要的是要了解模运算符 %
与 int
s.
正如πìντα ρέι 提到的,还有这个:Can't use modulus on doubles?
顺便说一句,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.