1

I am attempting to use a c code which i did not write myself in tandem with my own c++ code. When I attempt to compile the c code below I get an error. I am unsure what this c code is attempting to accomplish with the .xxx format. I am at a loss any help would be appreciated.

const bmpfile_header_t bmp_fh = {
        .filesz = offset + bmp_ih->bmp_bytesz,
        .creator1 = 0,
        .creator2 = 0,
        .bmp_offset = offset
    };

throws the error: missing'}' before '.'

user3592652
  • 15
  • 1
  • 5
  • Here is [a nice description][1] of the meaning of the 'dot's in the structure. [1]: http://stackoverflow.com/questions/330793/how-to-initialize-a-struct-in-ansi-c – jcoppens Jun 14 '15 at 13:40
  • 2
    Do not compile C code with a C++ compiler! These are two different languages which unfortunately share a lot of the **syntax**. However, many semantics are subtle different and may result in unexpected or even undefined behaviour. – too honest for this site Jun 14 '15 at 13:41
  • What compiler are you using? – Joe Jun 14 '15 at 13:45
  • You should compile this code with a C compiler. Use a C++ compiler to compile your C++ code. Then use a linker to link the two results. – M.M Jun 14 '15 at 14:21

3 Answers3

0

The .field = value are designated initializers for the fields of the struct, i.e., bmp_fh.bmp_offset is set to the value offset, etc. You could either give values for all fields of the structure (in order) without the designated (.field) initializers, or remove the const and assign the values afterwards.

Or, unless you specifically wish to port the C code to your C++ compiler, compile the C code with a C compiler and link it to your C++ program. C++ code can call C functions.

Arkku
  • 41,011
  • 10
  • 62
  • 84
0

These are designated intializers C++ does not support them. Use a constructor or remove them and specify the values in the order defined by the struct definition.

Note that .filesz = offset + bmp_ih->bmp_bytesz actually looks suspect, as an intializer in C is required to provide constant expressions which can be evaluated at compiler-time. However, gcc for instance allows such for local variables, but there const would only make little sense.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
0

You should compile the C code with a C compiler and link against it. To reference variables and functions you create a header file with declarations for them, surrounded by extern "C" { } and include that header in your C++ code.

Please note that C and C++ are, although similar, different languages and should thus be compiled with their respective compilers. The main purpose of the extern declaration is to specify a different way to link to the contained declarations.

As C and C++ compilers often are packaged together using the C compiler should not be a problem.

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63