identifier int not a direct member of struct SOCKE

2019-08-13 10:30发布

问题:

When I compile the below struct:-

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;

I get compilation error that:-

error (dplus:1384): identifier all_fields not a direct member of SOCKET_LOG_DATA

How do I fix this?

回答1:

I fixed this with proper constructor initialization and member variable placement as follows:-

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;

Thanks for your suggestions!