不支持非平凡的指定初始化器 [英] non-trivial designated initializers not supported

查看:58
本文介绍了不支持非平凡的指定初始化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结构如下:

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]

这篇关于不支持非平凡的指定初始化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆