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

Jac*_*kzz 3 c++ enums struct switch-statement

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

struct abc{
    enum xyz{
        FIRST =1;
        SEC =2;
    }qw;
};
Run Code Online (Sandbox Code Playgroud)

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

struct abc az;
switch(az.qw){
case FIRST:....
case SEC:...
default:..
}
Run Code Online (Sandbox Code Playgroud)

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

jua*_*nza 6

xyz在你的范围内定义abc,所以你需要

case abc::FIRST:
Run Code Online (Sandbox Code Playgroud)

等等