不平凡的指定初始值设定项不受支持 [英] non-trivial designated initializers not supported

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

问题描述

我的结构如下:

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。 (不推荐)或将此代码放入外部C [免责声明,我尚未测试过此代码]

2) Use g++ -X c. (Not recommended) or put this code in extern C [Disclaimer, I have not tested this]

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

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