18

我遇到了枚举数问题。让我们不要浪费任何人的时间,直接开始吧。错误:

1> forgelib\include\forge\socket.h(79): error C2365: 'RAW' : redefinition; previous definition was 'enumerator'
1>          forgelib\include\forge\socket.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
    };
}

是什么赋予了?

4

2 回答 2

27

在旧的 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;
};
于 2013-05-27T11:33:31.703 回答
4

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
      };
    }
}
于 2013-05-27T11:34:47.987 回答