重新定义和枚举器

Jes*_*nds 12 c++ enums visual-c++

我遇到了普查员的问题.让我们不要浪费任何人的时间,直接去做吧.错误:

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'
Run Code Online (Sandbox Code Playgroud)

代码:

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
    };
}
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?

For*_*veR 17

在旧的c风格枚举中你不能有相同的名字.如果你有C++ 11 - 你可以使用enum class,类中的静态常量,不同的命名空间,或者你可以简单地使用不同的名称.

用例子 enum classes

enum class SocketType
{
   RAW = SOCK_RAW
};

enum class ProtocolType
{
   RAW = IP_PROTO_RAW
};
Run Code Online (Sandbox Code Playgroud)

例子 constants

struct SocketType
{
   static const int RAW = SOCK_RAW;
};

struct ProtocolType
{
   static const int RAW = IP_PROTO_ROW;
};
Run Code Online (Sandbox Code Playgroud)

  • 所以尽管他们在不同的普查员中,我还是要重命名其中一个?这有多愚蠢.愚蠢的语言.谢谢. (3认同)
  • 不,您不必重命名它们。解决的办法就是他的答案。 (2认同)
  • @JesseBrands:想象一下这种语言会接受您的构造,`int x = RAW;`行到底会做什么? (2认同)
  • 我想我完全理解他的来历。我希望“ int x = RAW;”行是无效的语法。我个人希望它读为“ int x = SocketType :: RAW;”或“ int x = Forge :: SocketType :: RAW;” (2认同)

Pio*_*ycz 5

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
      };
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @JesseBrands 真的,同时说“语言太旧了”和“我们不使用语言的新形式”......很有趣。 (4认同)
  • @JesseBrands 那么谁处于石器时代?C++ 已经进入青铜时代整整两年了:-) (3认同)
  • 您可能认为它可以区分 Forge::SocketType::RAW 和 Forge::ProtocolType::RAW...但显然 C++ 仍处于石器时代。 (2认同)