重定义和枚举器
Redefinition and Enumerator
我遇到了枚举器的问题。我们不要浪费任何人的时间,直接开始吧。错误:
1> forgelibincludeforgesocket.h(79): error C2365: 'RAW' : redefinition; previous definition was 'enumerator'
1> forgelibincludeforgesocket.h(66) : see declaration of 'RAW'
代码:
namespace Forge {
enum SocketType {
STREAM = SOCK_STREAM, // Sequenced, reliable, 2-way
DGRAM = SOCK_DGRAM, // Connectionless, unreliable
RAW = SOCK_RAW, // Raw protocol
RDM = SOCK_RDM, // Reliable-delivered message
SEQPACKET = SOCK_SEQPACKET // Sequenced, reliable, 2-way
};
enum ProtocolType {
IP = IPPROTO_IP, // IPv4
ICMP = IPPROTO_ICMP, // Internet Control Messsage Protocol
IGMP = IPPROTO_IGMP, // Internet Group Management Protocol
GGP = IPPROTO_GGP, // Gateway to Gateway Protocol
TCP = IPPROTO_TCP, // Transmission Control Protocol
PUP = IPPROTO_PUP, // PARC Universal Packet Protocol
UDP = IPPROTO_UDP, // User Datagram Protocol
IDP = IPPROTO_IDP, // Xerox NS Protocol
RAW = IPPROTO_RAW, // Raw IP Packets
IPV6 = IPPROTO_IPV6 // IPv6
};
}
给了什么?
在旧的c风格枚举中不能有相等的名称。如果你有c++ 11 -你可以使用enum class
,类中的静态常量,不同的命名空间,或者你可以简单地使用不同的名称。
以enum classes
enum class SocketType
{
RAW = SOCK_RAW
};
enum class ProtocolType
{
RAW = IP_PROTO_RAW
};
以constants
为例
struct SocketType
{
static const int RAW = SOCK_RAW;
};
struct ProtocolType
{
static const int RAW = IP_PROTO_ROW;
};
Forge::RAW
是不明确的,不知道这是否来自哪个枚举类型。
使用这个样式:
namespace Forge {
namespace SocketType {
enum Values {
STREAM = SOCK_STREAM, // Sequenced, reliable, 2-way
DGRAM = SOCK_DGRAM, // Connectionless, unreliable
RAW = SOCK_RAW, // Raw protocol
RDM = SOCK_RDM, // Reliable-delivered message
SEQPACKET = SOCK_SEQPACKET // Sequenced, reliable, 2-way
};
}
namespace ProtocolType {
enum Values {
IP = IPPROTO_IP, // IPv4
ICMP = IPPROTO_ICMP, // Internet Control Messsage Protocol
IGMP = IPPROTO_IGMP, // Internet Group Management Protocol
GGP = IPPROTO_GGP, // Gateway to Gateway Protocol
TCP = IPPROTO_TCP, // Transmission Control Protocol
PUP = IPPROTO_PUP, // PARC Universal Packet Protocol
UDP = IPPROTO_UDP, // User Datagram Protocol
IDP = IPPROTO_IDP, // Xerox NS Protocol
RAW = IPPROTO_RAW, // Raw IP Packets
IPV6 = IPPROTO_IPV6 // IPv6
};
}
}
相关文章:
- 如何正确实现和访问运算符的各种自定义枚举器
- 枚举类'classname'的多重定义
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- C++:如何在声明枚举类的模板类主体之外定义枚举类?
- C2365 重新定义 VS2019 升级后,以前的定义是外部标头 + lib 的"枚举器"
- 如何在 std::p air 中使用 System::Guid 和自定义枚举?
- 如何理解c++中在命名空间内部定义的枚举类型enum
- 当在基类中定义枚举时,qml中的Q_ENUM未定义值
- 在模板类内部定义的枚举上的嵌套类的部分专用化
- 枚举的复合赋值运算符真的应该根据其关联的算术运算符来定义吗?
- 如何检查类中定义的枚举类型变量是否被分配给?
- 在 FlatBuffer 中使用自定义枚举
- C++:使用后类中的枚举定义
- 不接受C++枚举定义
- 方便地为枚举定义按位运算符,以创建位标志
- 为嵌套在类中的枚举定义运算符
- 为什么在c++枚举定义中需要typedef
- 在类中添加枚举定义会破坏其二进制向后兼容性吗?
- 扩展枚举定义
- 用枚举定义成员函数不需要Include