c++ - 枚举类 "could not convert to unsigned int"

标签 c++ casting enum-class

我有一个这样的枚举类:

    typedef unsigned int binary_instructions_t;

    enum class BinaryInstructions : binary_instructions_t
    {
        END_INSTRUCTION = 0x0,

        RESET,

        SET_STEP_TIME,
        SET_STOP_TIME,
        START,

        ADD
    };

我正尝试在这样的 switch 语句中使用枚举的成员:

const std::string& function(binary_instructions_t arg, bool& error_detect)
{
    switch(arg)
    {
        case (unsigned int)BinaryInstructions::END_INSTRUCTION:
            return "end";
        break;
    }
    translate_error = true;
    return "ERROR";
}

当基础类型已经是 unsigned int 时,为什么需要强制转换为 (unsigned int)

最佳答案

那是因为“枚举类”是“强类型”的,所以不能隐式转换为任何其他类型。 http://en.wikipedia.org/wiki/C%2B%2B11#Strongly_typed_enumerations

关于c++ - 枚举类 "could not convert to unsigned int",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14968029/

相关文章:

c++ - 我需要释放推力返回的 device_ptr 吗?

C++ 自动类型转换 : wrong behaviour for a container class

c - 如何在c中操作整数类型的位?

c++ - 是否可以将作用域枚举 ("enum class") 上下文转换为 bool 值?

c++ - 转发声明枚举类不起作用

c++ - 访问 vector 字段的键 - 命名空间中的枚举类或枚举?

C++:使用 nlohmann json 从文件中读取 json 对象

C++ 冒泡排序

c++ - C++代码中的字节序影响

c++ - 如何将 QList<QObject *> 转换为 QList<ADerivedQObjectClass*>