compound literal compilation error

after reading the introduction about it,i found it really amazing.

just so you guys know,i encounter a problem,see the codes below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
	typedef struct a {
		int x;
	    double y;
	}A;
	typedef union b{
		
		int h;
		double i;
	}B;
//	A a1;
	A a1 = (A){
	.x=67,
	.y=78.67
	};
//	union b b1;
	B	b1 = (union b){
		 .i=67.8,
	};
	return 0;
}

and the error messages are:
--------------------Configuration: mingw5 - CUI Debug, Builder Type: MinGW--------------------

Compiling H:\sync\C-Free\Temp\Untitled1.cpp...
[Error] H:\sync\C-Free\Temp\Untitled1.cpp:15: error: expected primary-expression before ')' token
[Error] H:\sync\C-Free\Temp\Untitled1.cpp:15: error: expected `,' or `;' before '{' token
[Error] H:\sync\C-Free\Temp\Untitled1.cpp:20: error: expected primary-expression before "union"
[Error] H:\sync\C-Free\Temp\Untitled1.cpp:20: error: expected `)' before "union"

Complete Compile H:\sync\C-Free\Temp\Untitled1.cpp: 4 error(s), 0 warning(s)

i wanna perform the member-wise initialization.

Thanks for any help !
Compund literals are a feature of C - not C++.
@guestgulkan

ok,just like you said , i modified the header to stdio.h, and the same errors were there.

On the other hand,isn't the c++ compiler compatible with the c compiler ?

Please do explain more,thanks in advance .
Last edited on
If you have to compile a C++ program use a C++ compiler, if you have to compile a C program use a C compiler.
Some compilers may detect the language from the source file extension
Get it, i use a c one and it works, now i feel it is hard to choose between this feature and those c++ particular features,because when i choose one ,i have to give up the other. Why don't c++ just adopt this?

Anyway,thanks you a lot!
Use constructors.
Topic archived. No new replies allowed.