错误:非法转换:从'int'到'union' [英] Error: illegal cast: from 'int' to 'union'
本文介绍了错误:非法转换:从'int'到'union'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到错误,非法转换:从'int'到'FIELDS'
,同时初始化结构变量: -
SOCKET_LOG_DATA socket_log_data():fields(0),socket_number(0){}
我应该如何解决?
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;
//按照@ Dietrich's& @crashmstrcomments: -
SOCKET_LOG_DATA():fields {{0,0}},socket_number(0){}
} SOCKET_LOG_DATA;
有很多错误: -
。filename.h,第183行:错误(dplus:1207):语法错误near
.filename.h,第183行:error ):arg-declaration-clause中预期的类型
.filename.h,行183:错误(dplus:1263):标识符socket_number已声明
.filename.h,行183:error (dplus:1376):function int socket_number(void)不是类$ incomplete的成员SOCKET_LOG_DATA
.filename.h,行183:错误(dplus:1247):语法错误字段后,expecting b $ b.filename.h,行183:错误(dplus:1404):mem初始化器只允许用于构造函数
.filename.h,行183:错误(dplus:1247) 0,expecting;
然后我保留
socket_log_data()$ c $通过将行改为
SOCKET_LOG_DATA socket_log_data():fields {{0,0}},socket_number {}
,并收到以下错误: -
。filename.h,第183行:错误(dplus:1272):member $ incomplete SOCKET_LOG_DATA ::非静态成员函数外使用的字段
.filename .h,行183:错误(dplus:1125):int常量期望
.filename.h,行183:错误(dplus:1536):位字段必须是整数类型
.filename .h,行183:错误(dplus:1247):字段后的语法错误,期望;
.filename.h,行183:错误(dplus:1436):语法错误 - 声明预期后}
.filename.h,行183:错误预期为socket_number
.filename.h,行183:错误(dplus:1247):语法错误after),expecting;
.filename.h,行186:错误(dplus:1461):SOCKET_LOG_DATA预期的类型
解决方案相关查询
我通过适当的构造函数初始化和成员变量放置修正了这一点: -
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;
感谢您的建议!
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 toSOCKET_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
解决方案Related query
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!
这篇关于错误:非法转换:从'int'到'union'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文