如何解决编译器枚举重新声明冲突

问题描述:

请考虑以下C ++枚举:

Consider the following C++ enumerations:

enum Identity
{
    UNKNOWN   = 1,
    CHECKED   = 2,
    UNCHECKED = 3
};

enum Status
{
    UNKNOWN    = 0,
    PENDING    = 1,
    APPROVED   = 2,
    UNAPPROVED = 3
};

编译器将 UNKNOWN 项与抛出此错误:

The Compiler conflicted the both UNKNOWN items and threw this error:


错误:重新声明了未知

error: redeclaration of 'UNKNOWN'

我能够解决将 UNKNOWN 之一更改为 UNKNOWN_a 的错误,但是我会

I am able to solve this error changing one of the UNKNOWN to UNKNOWN_a, but I would like to do not change the names.

如何在不更改枚举$ c的情况下解决冲突, $ c>项目名称?

How can I solve this conflict without changing the enum items name?

您可以使用作用域枚举。这需要C ++ 11或更高版本的支持。

You can use scoped enumerations for this. This requires C++11 or higher support.

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;
}

在线示例