不支持非平凡的指定初始化器
我的结构如下:
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;
};
当我尝试这样初始化它时:
When I try to initialize it thus:
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 是行不通的.
I think it wants the order of initialization exactly as it is declared, and local_connection
is missing. I don't need to initialize it though, and setting it to NULL doesn't work.
如果我将其更改为 g++,仍然会出现相同的错误:
If I change it to this for g++, still get the same error:
struct app_data data =
{
port : 18515,
ib_port : 1,
size : 65536,
tx_depth : 100,
sockfd : -1,
servername : NULL,
remote_connection : NULL,
ib_dev : NULL
};
推荐答案
这不适用于 g++.您实际上是在使用带有 C++ 的 C 构造.有几种方法可以绕过它.
This does not work with g++. You are essentially using C constructs with C++. Couple of ways to get around it.
1) 去掉."并在初始化时将="更改为:".
1) Remove the "." and change "=" to ":" when initializing.
#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;
}
2) 使用 g++ -X c.(不推荐)或者把这段代码放在extern C中【免责声明,我没有测试过】
2) Use g++ -X c. (Not recommended) or put this code in extern C [Disclaimer, I have not tested this]
相关文章