gpt4 book ai didi

c++ - 不支持非平凡的指定初始化器

转载 作者:IT老高 更新时间:2023-10-28 13:59:19 35 4
gpt4 key购买 nike

我的结构如下:

struct app_data
{
int port;
int ib_port;
unsigned size;
int tx_depth;
int sockfd;
char *servername;
struct ib_connection local_connection;
struct ib_connection *remote_connection;
struct ibv_device *ib_dev;

};

当我尝试这样初始化它时:

struct app_data data =
{
.port = 18515,
.ib_port = 1,
.size = 65536,
.tx_depth = 100,
.sockfd = -1,
.servername = NULL,
.remote_connection = NULL,
.ib_dev = NULL
};

我收到此错误:

sorry, unimplemented: non-trivial designated initializers not supported

我认为它希望初始化的顺序与声明的完全一致,并且缺少 local_connection。不过我不需要初始化它,设置为 NULL 是行不通的。

如果我将其更改为 g++,仍然会出现相同的错误:

struct app_data data =
{
port : 18515,
ib_port : 1,
size : 65536,
tx_depth : 100,
sockfd : -1,
servername : NULL,
remote_connection : NULL,
ib_dev : NULL
};

最佳答案

初始化的顺序需要与声明的顺序一致。

typedef struct FOO
{
int a;
int b;
int c;
}FOO;

FOO foo = {.a = 1, .b = 2}; // OK
FOO foo1 = {.a = 1}; // OK
FOO foo2 = {.b = 2, .a = 1}; // Error sorry, unimplemented: non-trivial designated initializers not supported
FOO foo3 = {.a = 1, .c = 2}; // Error sorry, unimplemented: non-trivial designated initializers not supported

我了解这意味着编译器不支持面向名称的无序成员初始化。

需要以老式方式初始化结构。为了清楚起见,我保留了变量名称,但我必须按顺序初始化它们,而不是跳过变量。

我可以在任何变量处停止初始化,但不能初始化由此产生的变量。

关于c++ - 不支持非平凡的指定初始化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215971/

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