C++ typedef 枚举 : Invalid conversion from int to enum

标签 c++ enums typedef

typedef enum{
 Adjust_mode_None = 0,
 Adjust_mode_H_min,
 Adjust_mode_H_max,
 Adjust_mode_S_min,
 Adjust_mode_S_max,
 Adjust_mode_V_min,
 Adjust_mode_V_max
}Adjust_mode;

在某些时候我想做:

adjust_mode_ = (adjust_mode_+1)%7; 

但是我明白了

Invalid conversion from int to Adjust_mode

这在其他语言中没问题,在 C++ 中有什么问题?我需要定义一些运算符吗?

最佳答案

使用static_cast。您需要显式转换。

adjust_mode_ = static_cast<Adjust_mode>(adjust_mode_+1)%7;

关于C++ typedef 枚举 : Invalid conversion from int to enum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861649/

相关文章:

java - 为什么 WindowConstants.EXIT_ON_CLOSE 真的是一个 int ?它不应该是一个枚举吗?

java - 在泛型上调用 Enum.values()

c - 如何正确转发声明 typedef 结构

c++ - 默认构造函数和 POD

c++ - 声明类的 C 函数友元并返回 C 枚举器

c++ - 无法将 char* 转换为 int*

c++ - 如何使用枚举有效填充 2D std::array

c - 传递一个二维数组给函数

c++ - 如何为参数为泛型类型的模板类中的方法定义函数签名

c++ - 使用 boost::asio 监听两个套接字