altendky
altendky

Reputation: 4354

C++ GNU designated structure initialization not recognized in Eclipse

The CDT parser reports a syntax error for the structure initialization:

typedef struct MyStruct
{
    int a;
    float b;
};

int main( void )
{
    // GNU C extension format
    MyStruct s = {a : 1, b : 2};
    // C99 standard format
//    MyStruct s = {.a = 1, .b = 2};

    return 0;
}

While GCC lists the : form as obsolete, it would seem that it has not been deprecated nor removed. In C99 I would certainly use the standard .<name> = form but for C++, the : is the only option that I am aware of for designated initialization.

I have tried setting my toolchain to both MinGW and Cross GCC, but neither seem to work.

How can I get Eclipse to recognize this syntax? It's not a big deal for one line but it carries through to every other instance of the variable since Eclipse does not realize it is declared.

Upvotes: 10

Views: 1459

Answers (2)

user4844465
user4844465

Reputation: 21

I meet this problems too and i use below method to solve it.

MyStruct s = {
 1,
 2,
}

This requires programmer to ensure sequence of initialization.

Upvotes: 1

Mark B
Mark B

Reputation: 96281

The . form is only available in C99 and not in any flavor of C++. In C++ your only standards-compliant options are ordered initialization or constructors.

You can use chaining with appropriate reference returning methods to create a similar interface (here a and b are methods rather than variables):

MyStruct s;
s.a(1).b(2);

Upvotes: 2

Related Questions