c++ - typedef 一个现有的枚举类型,可能吗?

标签 c++ enums typedef

鉴于以下情况:

namespace otherns
{
    enum MyEnum_e { MyEnum_YES, MyEnum_NO };
}

namespace myns
{
    typedef otherns::MyEnum_e MyEnum_e;
}

为什么以下内容无效?

int e = myns::MyEnum_YES;

我收到编译器错误说明:

'MyEnum_YES' is not a member of 'myns'

最佳答案

因为枚举值位于命名空间 otherns 中,而不是作为 MyEnum_e 的子项:要引用 MyEnum_YES,您键入 otherns::MyEnum_YES.

你可以试试这个:

namespace otherns
{
    namespace MyEnum_e_space {
    enum MyEnum_e { MyEnum_YES, MyEnum_NO };
    }
    using namespace MyEnum_e_space;
}

namespace myns
{
    using namespace otherns::MyEnum_e_space;
}

尽管不鼓励使用 using..

关于c++ - typedef 一个现有的枚举类型,可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5820159/

相关文章:

c++ - 如何确定 TCP 套接字因中间网络断开而终止所需的最长时间?

java - Spring MVC 枚举识别

c - 类型定义 uint8 x[4]

c - 在 C 编程中定义和初始化此数据类型

c++ - Win32 应用程序不在启动时运行

c++ - 如何访问链接列表的私有(private) vector

java - 枚举静态数组和注解值混淆

c++ - typedef a std::string - 最佳实践

c++ - 尝试使用参数包绑定(bind) std::function 时获取未声明的参数

Java枚举,根据bool值不同的值