Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefinition and Enumerator

I'm having a problem with enumerators. Let's not waste anyone's time, and get straight to it. The error:

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'

The code:

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
    };
}

What gives?

like image 290
Jesse Brands Avatar asked May 27 '13 11:05

Jesse Brands


People also ask

What is an enumerator in programming?

An enumeration, or Enum , is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.

What is a C++ enumerator?

Enum, which is also known as enumeration, is a user-defined data type that enables you to create a new data type that has a fixed range of possible values, and the variable can select one value from the set of values.


2 Answers

You cannot have equal names in old c-style enums. If you have C++11 - you can use enum class, static constants in classes, different namespaces, or you can simply use different names.

Example with enum classes

enum class SocketType
{
   RAW = SOCK_RAW
};

enum class ProtocolType
{
   RAW = IP_PROTO_RAW
};

example with constants

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

struct ProtocolType
{
   static const int RAW = IP_PROTO_ROW;
};
like image 54
ForEveR Avatar answered Sep 26 '22 09:09

ForEveR


Forge::RAW is ambiguous, it is not knows if this is from which enum type.

Use this style:

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
      };
    }
}
like image 39
PiotrNycz Avatar answered Sep 26 '22 09:09

PiotrNycz