7

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.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
Ashoka K
  • 453
  • 3
  • 6
  • 17

4 Answers4

4

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

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

should work fine.

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

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • But my requirement is i have to compile the source as C++ for ARMv7 embedded processors. Since other files were compiled with C++ options i have to compile the above mentioned code also in C++. It works with C Compiler, but i want it to compile in C++ :( – Ashoka K Jul 09 '12 at 13:10
  • Yes, I cant change the code. The above code is exact copy with member/structure names changed. Please help me to compile the source in C++. The exact Compile command i am using is : $ /bin/arm-none-linux-gnueabi-c++ -O3 -std=gnu++0x -march=armv7-a -mtune=cortex-a8 -mfpu=neon -ffast-math file.c – Ashoka K Jul 09 '12 at 13:18
  • Is it not compiling as C++? Even though this is written as C code, it looks like it should compile fine as C++ (except for the lines that were causing an error, but I'd never write that in C), although you may get warnings about your header names being deprecated. – Eric Finn Jul 09 '12 at 13:57
  • @Eric Finn: `except for the lines that were causing an error` -> Exactly. – Sebastian Mach Jul 09 '12 at 14:12
  • @AshokaK: If you insist on compiling the code as C++ the you will have to port it from C to C++. – CB Bailey Jul 09 '12 at 14:19
  • @AshokaK: You must change the code *or* compile it as C. *Why* do you have to compile it as C++? There shouldn't be a problem mixing C and C++ as long as the C declarations are `extern "C"` when C++ sees them, I think. – molbdnilo Jul 09 '12 at 14:31
1

Designated aggregate initializers is a C99 feature, i.e. it is a feature of C language. It is not present in C++.

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

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1
/* 
file1.c
*/
CONFIG_T  cfg =
{
  .str1 = { 0x01, 0x02 },
  .str2 = { 0x03, 0x04 },
  .str3 = { 0x05, 0x06 }
};

That code is using a C99 feature called designated initializers. As you have observed, that feature is not available in C++ and C++11.


As suggested in this answer you should use a C compiler for C code. You can still link it to your C++ application. You could use cmake to do 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 << t.i << " " << t.j << std::endl;
}

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

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

Community
  • 1
  • 1
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • @ moooeeeep ... Thanks for that. Will check on the solution. As somebody said "Hammers for nails, screwdrivers for screws ... For Humans ... ? :) :)" – Ashoka K Jul 10 '12 at 09:18
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

Ashoka K
  • 453
  • 3
  • 6
  • 17
  • you should accept [SingerOfTheFall](http://stackoverflow.com/a/11395350/1025391)'s answer then I think. – moooeeeep Jul 10 '12 at 13:14