如何修复 C/C++ round(): error: invalid operands of types 'float' and 'int' to binary 'operator&'

问题:

你正在尝试编译 C/C++ 程序,但你看到类似这样的错误消息

error_log.txt
src\main.cpp:357:21: error: invalid operands of types 'float' and 'int' to binary 'operator&'

指的是类似这样的行

fix_round_cast.c
long m = round(v) & 0x7FF;

解决方案

round() 的结果是浮点数。你正在尝试使用 & 运算符对 floatint(上例中的 0x7FF)执行按位 AND。但是,在 C/C++ 中不能对 float 执行按位操作。

为了修复此问题,将 round() 的结果转换为 int

fix_round_cast.c
long m = ((int)round(v)) & 0x7FF;

这应该修复编译器错误。


Check out similar posts by category: C/C++, GCC Errors