Iva*_*van 53 c++ initialization
我的结构如下:
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;
};
Run Code Online (Sandbox Code Playgroud)
当我尝试初始化它时:
struct app_data data =
{
.port = 18515,
.ib_port = 1,
.size = 65536,
.tx_depth = 100,
.sockfd = -1,
.servername = NULL,
.remote_connection = NULL,
.ib_dev = NULL
};
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
sorry, unimplemented: non-trivial designated initializers not supported
Run Code Online (Sandbox Code Playgroud)
我认为它需要完全按照声明的顺序进行初始化,并且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
};
Run Code Online (Sandbox Code Playgroud)
Par*_*rse 48
初始化的顺序需要按照声明的确切顺序.
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
Run Code Online (Sandbox Code Playgroud)
据我所知,这意味着编译器不支持面向名称的乱序成员初始化.
需要以老式的方式初始化结构.为了清楚起见,我保留变量名称,但是我必须按顺序初始化它们,而不是跳过变量.
我可以停止任何变量的初始化,但不能初始化由此产生的变量.
Ano*_*non 27
这不适用于g ++.您基本上使用C构造与C++.几种方法来绕过它.
1)删除"." 并在初始化时将"="更改为":".
#include <iostream>
using namespace std;
struct ib_connection
{
int x;
};
struct ibv_device
{
int y;
};
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;
};
int main()
{
struct app_data data =
{
port : 18515,
ib_port : 1,
size : 65536,
tx_depth : 100,
sockfd : -1,
servername : NULL,
local_connection : {5},
remote_connection : NULL,
ib_dev : NULL
};
cout << "Hello World" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
2)使用g ++ -X c.(不推荐)或将此代码放在外部C [免责声明,我还没有测试过这个]
flo*_*dis 11
我注意到我的GCC编译器有一些技巧可以接受.fieldname = value分配,但只有当字段的顺序与它们在结构中声明的顺序相同时才会编译.
我能够以两种方式初始化这个结构.具有名称的那个提高了可读性,并且如果稍后更改结构字段顺序,则降低了分配错误数据的风险.
//Declare struct
typedef struct
{
uint32_t const * p_start_addr;
uint32_t const * p_end_addr;
fs_cb_t const callback;
uint8_t const num_pages;
uint8_t const priority;
} fs_config_t;
//Assign unnamed
fs_config_t fs_config
{
(uint32_t*)0x00030000, // uint32_t const * p_start_addr;
(uint32_t*)0x00038000, // uint32_t const * p_end_addr;
fs_evt_handler, // fs_cb_t const callback;
8, // uint8_t const num_pages;
0xFE // uint8_t const priority;
};
//Assign to named fields
static fs_config_t fs_config1
{
.p_start_addr = (uint32_t*)0x00030000,
.p_end_addr = (uint32_t*)0x00038000,
.callback = fs_evt_handler,
.num_pages = 8,
.priority = 0xFE
};
Run Code Online (Sandbox Code Playgroud)
经验法则是:
由于在 Arduino IDE 中没有其他方法对我有用,我决定简单地分别设置每个字段:
struct app_data data;
data.port = 18515;
data.ib_port = 1;
data.size = 65536;
data.tx_depth = 100;
data.sockfd = -1;
data.servername = NULL;
data.remote_connection = NULL;
data.ib_dev = NULL;
Run Code Online (Sandbox Code Playgroud)