: invalid operands of types 'void' and 'int' to binary 'operator/'
时间: 2023-08-02 18:07:06 浏览: 756
这个错误通常表示您正在尝试将 `void` 类型的值与 `int` 类型的值进行除法操作,而除法操作需要两个具有数值类型的操作数。`void` 类型表示没有返回值的函数或表达式,不允许进行算术运算。
要解决这个错误,您需要确保除法操作的两个操作数都是数值类型。如果其中一个操作数是 `void` 类型,您需要找出导致该操作数为 `void` 类型的原因,并进行修正。
相关问题
error: invalid operands of types ‘double’ and ‘int’ to binary ‘operator%’
错误信息 "error: invalid operands of types 'double' and 'int' to binary 'operator%' " 意味着在 C++ 或其他支持运算符 % 的编程语言中,尝试对一个 double 类型的值(浮点数)和一个 int 类型的值进行取模(%)操作。取模运算通常用于整数之间,返回除法的余数。由于 double 和 int 的数据类型不兼容,这种操作在编译阶段是不允许的。
如果你试图对它们求模,你需要确保两个操作数都是整数类型。例如,你可以将 double 转换为 int 再做取模,或者直接使用两个整数进行运算。修复这个问题的方法如下:
```cpp
// 如果 double 变量需要转换
int intVal = static_cast<int>(yourDoubleValue);
int result = intVal % anotherInt;
// 或者如果 int 变量不变,直接转换 double 到整数
result = static_cast<int>(yourDoubleValue) % yourOtherInt;
```
Qt5.12.4 中报错:invalid operands of types ‘qreal{aka double}’ and ‘double’ to binary ‘operator%’
在Qt 5.12.4 版本中,遇到 "invalid operands of types 'qreal (aka double)' and 'double' to binary 'operator%' 错误通常是由于你在尝试对`qreal`类型(一种增强版的双精度浮点数)和`double`类型进行取余操作,而`%`运算符在C++标准库中默认只适用于同种类型的数值。`qreal`和`double`虽然可以隐式转换,但是在进行取余运算时,Qt可能期望你明确指定一个支持该操作的操作数类型。
解决这个问题的方法有两种:
1. 如果你需要确切地对`double`值进行取余操作,你应该将其中一个值转换为`qreal`类型,然后再进行运算。例如:
```cpp
qreal qrealValue = yourDoubleValue;
double doubleValue = otherDoubleValue;
qreal remainder = qrealValue % doubleValue; // 或者 vice versa
```
2. 如果你确信`double`值不会影响程序的行为,也可以直接在`double`类型之间进行取余,但最好在使用前检查是否真的不需要`qreal`的特性。
总之,确保操作数类型匹配或适当地进行类型转换是避免这个错误的关键。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)