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

时间:2015-02-19 08:06:52

标签: c++ struct enums switch-statement

我有一个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错误。如何克服这个问题。

1 个答案:

答案 0 :(得分:6)

xyzabc的范围内定义,因此您需要

case abc::FIRST: