重定义和枚举 [英] Redefinition and Enumerator

查看:806
本文介绍了重定义和枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有枚举器的问题。让我们不要浪费任何人的时间,直接得到它。错误:

  1> forgelib \include\forge\socket.h(79):错误C2365:'RAW':redefinition;以前的定义是'enumerator'
1> forgelib \include\forge\socket.h(66):参见RAW的声明

代码:

 命名空间Forge {
enum SocketType {
STREAM = SOCK_STREAM,// Sequenced,可靠,双向
DGRAM = SOCK_DGRAM,//无连接,不可靠
RAW = SOCK_RAW,//原始协议
RDM = SOCK_RDM,//可靠传递消息
SEQPACKET = SOCK_SEQPACKET // Sequenced,reliable,2-way
};
enum ProtocolType {
IP = IPPROTO_IP,// IPv4
ICMP = IPPROTO_ICMP,// Internet控制消息协议
IGMP = IPPROTO_IGMP,//互联网组管理协议
GGP = IPPROTO_GGP,//网关到网关协议
TCP = IPPROTO_TCP,//传输控制协议
PUP = IPPROTO_PUP,// PARC通用分组协议
UDP = IPPROTO_UDP //用户数据报协议
IDP = IPPROTO_IDP,// Xerox NS协议
RAW = IPPROTO_RAW,//原始IP包
IPV6 = IPPROTO_IPV6 // IPv6
}什么给了?


h2_lin>解决方案

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



使用枚举类的示例

 枚举class SocketType 
{
RAW = SOCK_RAW
};

enum class ProtocolType
{
RAW = IP_PROTO_RAW
};

例如常数

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

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


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?

解决方案

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

这篇关于重定义和枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆