2013-05-27 15 views
8

Ich habe ein Problem mit Enumeratoren. Lass uns nicht die Zeit verschwenden und gleich damit anfangen. Der Fehler:Redefinition und Enumerator

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' 

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

Was soll das?

Antwort

12

Sie können in alten C-Style Enums keine gleichen Namen haben. Wenn Sie C++ 11 haben - können Sie enum class, statische Konstanten in Klassen, verschiedene Namespaces verwenden, oder Sie können einfach andere Namen verwenden.

Beispiel mit enum classes

enum class SocketType 
{ 
    RAW = SOCK_RAW 
}; 

enum class ProtocolType 
{ 
    RAW = IP_PROTO_RAW 
}; 

Beispiel mit constants

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

struct ProtocolType 
{ 
    static const int RAW = IP_PROTO_ROW; 
}; 
+2

So habe ich einen von ihnen zu benennen, obwohl sie in verschiedenen Aufzählungen zu sein? Wie dumm ist das? Dumme Sprache. Vielen Dank. –

+1

Nein, Sie müssen sie nicht umbenennen. Die Lösung ist in seiner Antwort. – Wilbert

+1

@JesseBrands: Stellen Sie sich vor die Sprache würde Ihr Konstrukt akzeptieren, was würde die Zeile 'int x = RAW;' genau tun? – PlasmaHH

2

Forge::RAW ist nicht eindeutig, es ist nicht weiß, ob dies aus welchem ​​Aufzählungstyp ist.

Mit diesem Stil:

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

Sie würden denken, dass es eine Unterscheidung zwischen Forge :: SocketType :: RAW und machen könnte Forge :: ProtocolType :: RAW ... aber anscheinend ist C++ noch in der Steinzeit. –

+0

Eigentlich ist es nicht, Sie haben nur keine Enum-Klassen verwendet. – Wilbert

+0

Verwenden von C++ 11 ist keine Option für dieses Projekt. Aber Piotrs Antwort reicht, danke! –

Verwandte Themen