Error: illegal cast: from 'int' to 'union'

只愿长相守 提交于 2020-01-03 01:34:10

问题


I'm getting the error, illegal cast: from 'int' to 'FIELDS' while initializing the structure variables here:-

SOCKET_LOG_DATA socket_log_data() : fields(0), socket_number(0) {}

How should I resolve it?

typedef PACKED struct PACKED_SUFFIX
{
      UINT16 loss_reason : 1;
      UINT16 unused : 15;
} LOSS_REASON;

typedef union PACKED_SUFFIX
{
      LOSS_REASON loss;
      UINT16 all_fields;
} FIELDS;

typedef PACKED struct PACKED_SUFFIX SOCKET_LOG_DATA
{
      FIELDS fields;
      UINT16 socket_number;

      // As per @Dietrich's & @crashmstrcomments:-
      SOCKET_LOG_DATA() : fields{{0, 0}}, socket_number(0) {}
} SOCKET_LOG_DATA;

Gave a lot many errors:-

".filename.h", line 183: error (dplus:1207): syntax error near }
".filename.h", line 183: error (dplus:1463): type expected in arg-declaration-clause
".filename.h", line 183: error (dplus:1263): identifier socket_number already declared
".filename.h", line 183: error (dplus:1376): function int socket_number(void) is not a member of class $incomplete SOCKET_LOG_DATA
".filename.h", line 183: error (dplus:1247): syntax error after fields, expecting (
".filename.h", line 183: error (dplus:1404): mem initializers only allowed for constructors
".filename.h", line 183: error (dplus:1247): syntax error after 0, expecting ;

Then I retained socket_log_data() constructor by changing the line to

SOCKET_LOG_DATA socket_log_data() : fields{{0, 0}}, socket_number(0) {}

, and received following errors:-

".filename.h", line 183: error (dplus:1272): member $incomplete SOCKET_LOG_DATA::fields used outside non-static member function
".filename.h", line 183: error (dplus:1125): int constant expected
".filename.h", line 183: error (dplus:1536): bitfields must be integral type
".filename.h", line 183: error (dplus:1247): syntax error after fields, expecting ;
".filename.h", line 183: error (dplus:1436): syntax error - declarator expected after }
".filename.h", line 183: error (dplus:1461): type expected for socket_number
".filename.h", line 183: error (dplus:1247): syntax error after ), expecting ;
".filename.h", line 186: error (dplus:1461): type expected for SOCKET_LOG_DATA

回答1:


You are initializing a union with a single int:

: fields(0)

You can initialize the first union member like this, instead:

: fields{{0, 0}}

There's also something fishy with the constructor:

SOCKET_LOG_DATA socket_log_data() ...

Ordinarily, it would just be:

SOCKET_LOG_DATA() ...



回答2:


Related query

http://stackoverflow.com/questions/35549540/identifier-int-not-a-direct-member-of-struct-socket-log-data/35555783#35555783

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!



来源:https://stackoverflow.com/questions/35485404/error-illegal-cast-from-int-to-union

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!