使用结构中定义的枚举作为C++中的大小写常量

Use enum defined in a struct as case constant in C++

本文关键字:C++ 大小写 常量 枚举 定义 结构      更新时间:2023-10-16

我有一个enum作为头文件中定义的structure的成员。例如,

struct abc{
    enum xyz{
        FIRST =1;
        SEC =2;
    }qw;
};

在我的.cpp文件中,我已经包含了这个标头。我的文件中有一个switch case,这些enums将用作case constants

struct abc az;
switch(az.qw){
case FIRST:....
case SEC:...
default:..
}

但我得到错误为FIRST is not declared in this scope。如何克服这个问题。

xyz是在abc的范围内定义的,因此您需要

case abc::FIRST:

等等。