C++ typedef现有枚举类型,是否可能?
鉴于以下情况:C++ typedef现有枚举类型,是否可能?,c++,enums,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中,而不
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\u e
的子项:要引用MyEnum\u YES
,请键入otherns::MyEnum\u 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
。因为枚举值位于命名空间otherns
中,而不是作为MyEnum\u e
的子项:要引用MyEnum\u YES
,请键入otherns::MyEnum\u 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;
}
虽然不鼓励使用
使用;经验法则是:不在头文件中(因为您不想在头文件的任何未来客户机上造成名称空间污染甚至符号解析冲突)。在您自己的编译单元中随意使用和。不要气馁,明智地使用它;经验法则是:不在头文件中(因为您不想在头文件的任何未来客户机上造成名称空间污染甚至符号解析冲突)。您可以在自己的编译单元中随意使用和