来自另一个类的switch语句中的static const int导致错误C2051:case表达式不是常量

Pao*_*aJ. 3 c++ static compiler-errors syntax-error switch-statement

我有一个简单的类

class Person {
  static const int MALE; // in Person.cpp initialized = 1
  static const int FEMALE;//in Person.cpp initialized = 2
};
Run Code Online (Sandbox Code Playgroud)

在公司类(Company.cpp文件中,我有公司类)我有switch的功能

 switch(x){// x is int passed as parameter to function
        case Person::MALE:
            //do something
        break;
        case Person::FEMALE:
            //do something
        break;
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试构建时error C2051: case expression not constant,如果在上面的开关中我遇到错误 ,那么当它是const时会出现什么问题?

Vla*_*cow 10

通过以下方式更改静态数据成员的声明

class Person {
  static const int MALE = 1;
  static const int FEMALE = 2;
};
Run Code Online (Sandbox Code Playgroud)

编译器必须在编译期间知道案例标签的值.