Alex Rivera | Logout

C++ error: expected primary-expression before ‘.’ token

Asked 2012-07-09T12:44:47.443
8

I looked at the earlier questions but still i was not satisfied, hence i am posting this. I was trying to compile the C++ code written by someone else.

/*
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 }
};

Compiled with std C++11 and i get below error. Why the '.' has been used in code while assigning values ?

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

I was not able to understand the reason for error. Please help.

Edit
Report

1 Answer

0

Thanks to all ...

After all the analysis i found that the above code has C99 feature called
Designated initializer.

To compile this code in C++ i have changed the code to normal initialization as below.

==========================

/*
 *  initialze_list.cpp 
 */

#include <stdio.h>

typedef struct
{
    struct
{   unsigned member1;
    unsigned  member2; 
} str1;
struct
{   unsigned member3;
    unsigned  member4; 
} str2;
struct
{   unsigned member5;
    unsigned  member6; 
} str3;
} CONFIG_T;

CONFIG_T  cfg =
{
 { 0x01, 0x02 },
 { 0x03, 0x04 },
 { 0x05, 0x06 }
};
/* End of file  */

==================================

This code compiled properly without the error in C++.

$$ g++ -c initialze_list.cpp

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

answered 2012-07-10T08:37:02.837

Your Answer