C ++中的C结构,不支持非平凡的指定初始值设定项

时间:2015-05-05 21:52:18

标签: c++ c gcc struct g++

所以我想在我的C ++程序中添加一个C库,并且遇到如何正确定义在C库中声明的结构的问题。 库中的示例程序以您在下面看到的方式定义结构。这已经让我立刻感到迷惑它的两个结构,或者至少这对我来说就是这样。我对C没有经验。

当我在C ++代码中包含示例程序中的这个片段时,我得到了这个编译错误:

src/main.cpp:47:4: sorry, unimplemented: non-trivial designated initializers not supported
    };
    ^

到目前为止,我的研究结果表明,使用C99或更高版本的问题很简单。虽然我在编译C ++代码时不确定如何定义C99或C11。我正在使用g ++ 4.8编译-std=c++0x。 当我在make文件中使用-std = c11添加cflag时,我收到一条警告:该标志不适用于我的代码,因为它只对C代码有效。

如果无法以这种方式使用结构,我将如何在C ++中正确定义它?

结构:

ws2811_t ledstring =
    {
        .freq = TARGET_FREQ,
        .dmanum = DMA,
        .channel =
        {
            [0] =
            {
                .gpionum = GPIO_PIN,
                .count = LED_COUNT,
                .invert = 0,
                .brightness = 255,
            },
            [1] =
            {
                .gpionum = 0,
                .count = 0,
                .invert = 0,
                .brightness = 0,
            },
        },
    };

C库的标题中包含以下结构:

typedef struct
{
    int gpionum;                                 //< GPIO Pin with PWM alternate function, 0 if unused
    int invert;                                  //< Invert output signal
    int count;                                   //< Number of LEDs, 0 if channel is unused
    int brightness;                              //< Brightness value between 0 and 255
    ws2811_led_t *leds;                          //< LED buffers, allocated by driver based on count
} ws2811_channel_t;

typedef struct
{
    struct ws2811_device *device;                //< Private data for driver use
    uint32_t freq;                               //< Required output frequency
    int dmanum;                                  //< DMA number _not_ already in use
    ws2811_channel_t channel[RPI_PWM_CHANNELS];
} ws2811_t;

0 个答案:

没有答案