C++错误:'.'之前的预期primary-expression 代币

Ash*_*a K 7 c++ initialization c++11

我看了之前的问题,但仍然不满意,因此我发布了这个.我试图编译其他人编写的C++代码.

/*
file1.h
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
    struct
    {   
        unsigned member1;
        unsigned  member2; 
    } str1;

    struct
    {
        unsigned member3;
        unsigned  member4; 
    } str2;

    struct
    {
        unsigned member5;
        unsigned  member6; 
    } str3;
} CONFIG_T;



/* 
file1.c
*/
CONFIG_T  cfg =
{
    .str1 = { 0x01, 0x02 },
    .str2 = { 0x03, 0x04 },
    .str3 = { 0x05, 0x06 }
};
Run Code Online (Sandbox Code Playgroud)

用std C++ 11编译,我得到以下错误.为什么 '.' 在分配值时是否已在代码中使用?

home $$  g++ -c -std=gnu++0x  initialze_list.cpp

initialze_list.cpp:34: error: expected primary-expression before ‘.’ token

initialze_list.cpp:35: error: expected primary-expression before ‘.’ token

initialze_list.cpp:36: error: expected primary-expression before ‘.’ token
Run Code Online (Sandbox Code Playgroud)

我无法理解错误的原因.请帮忙.

Sin*_*all 4

您发布的是 C 代码,而不是 C++ 代码(请注意 .c 文件扩展名)。然而,下面的代码:

CONFIG_T  cfg =
{
    { 0x01, 0x02 },
    { 0x03, 0x04 },
    { 0x05, 0x06 }
};
Run Code Online (Sandbox Code Playgroud)

应该可以正常工作。

您还可以在wiki中阅读有关 C++11 初始化列表的信息。