错误:“表达式必须具有整数或未整合的枚举类型”

时间:2016-09-20 19:29:11

标签: c++ math enums operators modulus

所以,我是新手我尝试寻找一个解决方案但是,我并没有真正理解它,我正在写速记来了解它们如何被其他代码替换,所以我遇到了我添加的模数然而它给出“表达式必须具有整数或未结合的枚举类型”

我不知道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;
}

这里的问题是“c”表示“表达式必须具有整数或未整合的枚举类型”错误。

我知道模数的作用,它给出了2个数之间除数的余数,但是我在这种情况下难倒,因为它应该给出余数吗?它在语法上是错误的吗?

1 个答案:

答案 0 :(得分:10)

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.