与C++ 11枚举的位操作
我有这个C ++ 11 类
,其中包含各种enum
s。这些enum
s包含 bitmasks ,最终使用 bitwise Operators 将所有最终都合并为8位值。
看起来像这样:
class SX1509
{
public:
enum class DriverMode
{
LINEAR = 0b00000000,
LOGARITHMIC = 0b10000000
};
enum class ClockSpeed
{
ULTRA_FAST = 0b00010000,
EXTRA_FAST = 0b00100000,
FAST = 0b00110000,
MEDIUM = 0b01000000,
SLOW = 0b01010000,
EXTRA_SLOW = 0b01100000,
ULTRA_SLOW = 0b01110000,
};
SX1509()
{
this->config(DriverMode::LOGARITHMIC, ClockSpeed::EXTRA_FAST)
};
void config(DriverMode driverMode, ClockSpeed clockSpeed) {
uint8_t config = driverMode | clockSpeed;
i2c->write(config);
}
}
但是,在将所有enum
定义更改为enum class
之后,编译器吐出了此错误,我不明白,这
error: no match for 'operator|' (operand types are 'SX1509::DriverMode' and 'SX1509::ClockSpeed')
是在这里发生了什么?
I have this c++11 class
which contains a variety of enum
s. These enum
s contain bitmasks which eventual all get combined together into an 8-bit values using bitwise operators.
It looks like this:
class SX1509
{
public:
enum class DriverMode
{
LINEAR = 0b00000000,
LOGARITHMIC = 0b10000000
};
enum class ClockSpeed
{
ULTRA_FAST = 0b00010000,
EXTRA_FAST = 0b00100000,
FAST = 0b00110000,
MEDIUM = 0b01000000,
SLOW = 0b01010000,
EXTRA_SLOW = 0b01100000,
ULTRA_SLOW = 0b01110000,
};
SX1509()
{
this->config(DriverMode::LOGARITHMIC, ClockSpeed::EXTRA_FAST)
};
void config(DriverMode driverMode, ClockSpeed clockSpeed) {
uint8_t config = driverMode | clockSpeed;
i2c->write(config);
}
}
However, after changing all my enum
definitions to enum class
the compiler spit out this error which I do not understand ????
error: no match for 'operator|' (operand types are 'SX1509::DriverMode' and 'SX1509::ClockSpeed')
What is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
枚举类
枚举不会被隐式转换为整数,这与常规枚举不同。我至少要做两件事。首先,明确指示
enum类的基本类型
,例如:虽然这无助于明确的转换,但由于从签名到未签名的转换,它将有助于扭转。
其次,明确转换枚举值,例如,
您可能需要使用
typedef
别名uint8_t
,例如,而不是使用RAW
uint> uint8_t
。enum class
enumerations do not get implicitly converted to integers, unlike regular enumerations.I would do at least two things. First, explicitly indicate the underlying type of the
enum class
, e.g.:While this won't help with explicit conversion, it will help with wonkiness due to conversion from signed to unsigned.
Second, explicitly convert the enumeration values, e.g.,
You may want to use a
typedef
alias foruint8_t
, e.g.,instead of using the raw
uint8_t
.