2013-05-27 13 views
8

Numaralandırıcılar ile ilgili bir sorun yaşıyorum. Kimsenin zamanını boşa harcamayın ve doğrudan ona ulaşın. Hata:Redefinition and 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' 

kodu: Ne verir

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

?

cevap

12

Eski c stili enumlarda eşit isimlere sahip olamazsınız. C++ 11'iniz varsa, enum class, sınıflardaki statik sabitleri, farklı ad alanlarını kullanabilir veya farklı adlar kullanabilirsiniz. constants

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

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

yüzden onları farklı Enumerator'lar olmasına rağmen bunlardan birini yeniden adlandırmak zorunda? Bu ne kadar aptalca. Aptal dil. Teşekkür ederim. –

+1

Hayır, onları yeniden adlandırmanız gerekmez. Çözüm onun cevabında. – Wilbert

+1

@JesseBrands: Dilin sizin yapınızı kabul edeceğini düşünün, 'int x = RAW' satırı ne yapar? – PlasmaHH

2

Forge::RAW ile enum classes

enum class SocketType 
{ 
    RAW = SOCK_RAW 
}; 

enum class ProtocolType 
{ 
    RAW = IP_PROTO_RAW 
}; 

örnekle

örnek Bu hangi enum türünden ise, o bilir değildir belirsizdir.

kullanın bu stili:

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

'un okunmasını beklerim. Forge :: SocketType :: RAW arasında bir ayrım yapabileceğini düşünebilirsiniz. Forge :: ProtocolType :: RAW ... ama görünüşe göre C++ hala taş devrede. –

+0

Aslında, enum sınıflarını kullanmamaya karar verdiniz. – Wilbert

+0

C++ 11'i kullanmak bu proje için bir seçenek değildir. Ama Piotr'ın cevabı yapacak, teşekkürler! –