C ++ error: expected primary expression before '. sign

I looked at earlier questions, but still I was not satisfied, so I am posting this. I tried to compile 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 errors. Why '.' was used in the code when 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 could not understand the cause of the error. Please, help.

+7
source share
4 answers

What you posted is C code, not C ++ code (note the .c file extension). However, the following code:

 CONFIG_T cfg = { { 0x01, 0x02 }, { 0x03, 0x04 }, { 0x05, 0x06 } }; 

should work fine.

You can also read about C ++ 11 initialization lists in the wiki .

+3
source

Assigned aggregate initializers are a function of C99, that is, a C-language function. It is not in C ++.

If you insist on compiling it as C ++, you will have to rewrite the initialization of cfg .

+1
source
 /* file1.c */ CONFIG_T cfg = { .str1 = { 0x01, 0x02 }, .str2 = { 0x03, 0x04 }, .str3 = { 0x05, 0x06 } }; 

This code uses function C99 called designated initializers . As you noticed, this function is not available in C ++ and C ++ 11.


As indicated in this answer , you should use the C compiler for the C code. You can still associate it with your C ++ application. You can use cmake to complete the build configuration for you. A simple example:

 /* f1.h */ #ifndef MYHEADER #define MYHEADER typedef struct { int i, j; } test_t; extern test_t t; #endif 

 /* f1.c */ #include "f1.h" test_t t = { .i = 5, .j = 6 }; 

 /* f0.cc */ extern "C" { #include "f1.h" } #include <iostream> int main() { std::cout << ti << " " << tj << std::endl; } 

 # CMakeLists.txt add_executable(my_executable f0.cc f1.c) 

just run mkdir build; cd build; cmake ..; make mkdir build; cd build; cmake ..; make mkdir build; cd build; cmake ..; make from your source directory.

+1
source

Thanks everyone ...

After all the analysis, I found that the above code has a C99 function called Assigned Initializer .

To compile this code in C ++, I changed the code to normal initialization, as shown 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 correctly without error in C ++.

$$ g ++ -c initialze_list.cpp

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

-one
source

All Articles