不支持非平凡的指定初始值设定项

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)

据我所知,这意味着编译器不支持面向名称的乱序成员初始化.

需要以老式的方式初始化结构.为了清楚起见,我保留变量名称,但是我必须按顺序初始化它们,而不是跳过变量.

我可以停止任何变量的初始化,但不能初始化由此产生的变量.

  • 与接受的答案的声明相反,这在g ++中不起作用,我成功地使用g ++(5.3.0)和本答案中给出的解决方案(而不是使用"-X"). (6认同)
  • 这个答案对我有用.我可以通过字段名称进行初始化,但必须按照我声明的顺序进行初始化. (6认同)

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 [免责声明,我还没有测试过这个]

  • 谢谢你的工作.顺便说一下,这些圆点在C++中工作.不知道为什么你建议":"而不是.至于C的用法,主要是我有C语言的例子,我试图将它移植到C++.一心一意. (6认同)
  • `extern"C"`不改变语言,只改变链接.即它告诉C++编译器编译C++代码,使其可以与来自C编译器的cod链接. (3认同)
  • 对于g ++,这不是我的正确答案。相反,结果证明该解决方案缺少初始化程序中的字段,并且字段混乱。似乎gcc和clang都比较宽大,只有g ++有此限制。 (3认同)
  • 正如@MSalters所说,即使在`extern“ C”`中,我也会遇到相同的错误。 (2认同)

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)

经验法则是:

  1. 分配给.name = value字段
  2. 按照声明的顺序分配
  3. 包括分配中的所有字段


Fal*_*lko 6

由于在 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)