How solve compiler enum redeclaration conflict(如何解决编译器枚举重声明冲突)
问题描述
考虑以下 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:
错误:重新声明UNKNOWN"
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.
如何在不更改enum项名称的情况下解决此冲突?
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;
}
现场示例
这篇关于如何解决编译器枚举重声明冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何解决编译器枚举重声明冲突
 
				
         
 
            
        - 如何对自定义类的向量使用std::find()? 2022-11-07
- 静态初始化顺序失败 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- C++ 协变模板 2021-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
 
						 
						 
						 
						 
						 
				 
				 
				 
				