identifier int不是struct SOCKET_LOG_DATA的直接成员

时间:2016-02-22 09:05:10

标签: c++ data-structures compiler-errors member unions

当我编译以下结构时: -

#import "Singleton.h"
#import "YourObjectClass.h"

@implementation Singleton

static Singleton *sharedObject;

+ (Singleton *)sharedInstance;
{
    if (sharedObject == nil) {
        static dispatch_once_t pred;
        dispatch_once(&pred, ^{
            sharedObject = [[Singleton alloc] init];
        });
    }
    return sharedObject;
}

+ (YourObjectClass *)getYourObject
{
    Singleton *singleton = [Singleton sharedInstance];
    return singleton.yourObject;
}

+ (void)setYourObject:(YourObjectClass *)yourObject
{
    Singleton *singleton = [Singleton sharedInstance];
    singleton.yourObject = yourObject;
}

我收到编译错误: -

typedef PACKED struct PACKED_SUFFIX SOCKET_LOG_DATA
{
      typedef PACKED union PACKED_SUFFIX
      {
            PACKED struct PACKED_SUFFIX
            {
                  UINT16 loss_reason : 1;
                  UINT16 unused : 15;
            } fields;
            UINT16 all_fields;
      } ;
      UINT16 socket_number;
      SOCKET_LOG_DATA () : all_fields(0), socket_number(0) {}
} SOCKET_LOG_DATA;

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我通过适当的构造函数初始化和成员变量放置修复了这个问题,如下所示: -

typedef struct fields
{
    UINT16 loss_reason : 1;
    UINT16 unused : 15;
} FIELDS;

typedef union fields_union
{
    UINT16 all_fields;
    FIELDS ref_fields;
    fields_union() : all_fields(0), ref_fields() {}
} FIELDS_UNION;

typedef struct socket_log_data
{
    FIELDS_UNION ref_fields_union;
    UINT16 socket_number;
    socket_log_data() : socket_number(0), ref_fields_union() {}
} SOCKET_LOG_DATA;

感谢您的建议!