请考虑以下C++枚举:
enum Identity { UNKNOWN = 1, CHECKED = 2, UNCHECKED = 3 }; enum Status { UNKNOWN = 0, PENDING = 1, APPROVED = 2, UNAPPROVED = 3 };
编译器将这两个UNKNOWN
项冲突并抛出此错误:
错误:重新声明'UNKNOWN'
我能够解决这个错误改变的一个UNKNOWN
来UNKNOWN_a
,但我想不会更改名称.
如何在不更改enum
项目名称的情况下解决此冲突?
您可以为此使用范围枚举.这需要C++ 11或更高版本的支持.
enum class Identity { UNKNOWN = 1, CHECKED = 2, UNCHECKED =3 }; enum class Status { UNKNOWN = 0, PENDING = 1, APPROVED = 2, UNAPPROVED =3 }; int main () { Identity::UNKNOWN; Status::UNKNOW; }
Live Example