gpt4 book ai didi

c++ - 错误 : illegal cast: from 'int' to 'union'

转载 作者:行者123 更新时间:2023-11-30 00:47:15 25 4
gpt4 key购买 nike

我在此处初始化结构变量时遇到错误,illegal cast: from 'int' to '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;

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

给出了很多错误:-

".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 ;

然后我通过将行更改为

来保留 socket_log_data() 构造函数
SOCKET_LOG_DATA socket_log_data() : fields{{0, 0}}, socket_number(0) {}

,并收到以下错误:-

".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

最佳答案

您正在使用单个 int 初始化一个 union:

: fields(0)

您可以像这样初始化第一个 union 成员,而不是:

: fields{{0, 0}}

构造函数也有问题:

SOCKET_LOG_DATA socket_log_data() ...

通常,它只是:

SOCKET_LOG_DATA() ...

关于c++ - 错误 : illegal cast: from 'int' to 'union' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35485404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com