typedef现有的枚举类型,可能吗?

时间:2011-04-28 14:25:26

标签: 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'

1 个答案:

答案 0 :(得分:4)

因为枚举值存在于名称空间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 ..