non-trivial designated initializers not supported(不支持非平凡的指定初始化器)
问题描述
我的结构如下:
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]
这篇关于不支持非平凡的指定初始化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不支持非平凡的指定初始化器


基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01