newbie c++ question

J

junky_fellow

guys,

I have a code that has to be compiled with C and C++ compiler both.
WIth C, I don't get any error but with C++, I am getting following
error.

error C2059: syntax error : '{'

Here is the code:
--------------------------------------------------------

#include <stdio.h>
#include <string.h>

typedef unsigned char uint8;
typedef struct {
uint8 b[16];
} efiGuid;

#define EFI_GUID(a, b, c, d, e0, e1, e2, e3, e4, e5) \
((efiGuid) \
{{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) &
0xff, \
(b) & 0xff, ((b) >> 8) & 0xff, \
(c) & 0xff, ((c) >> 8) & 0xff, \
(d) & 0xff, ((d) >> 8) & 0xff, \
(e0), (e1), (e2), (e3), (e4), (e5) }})

#define GUID_ABC \
EFI_GUID(0x00000000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00,
0x00, 0x00, 0
x00)
#define GUID_XYZ \
EFI_GUID(0xc12a7328, 0xf81f, 0x11d2, 0xba4b, 0x00, 0xa0, 0xc9,
0x3e, 0xc9, 0
x3b)


int main(void)
{
typedef struct {
unsigned int time_low;
unsigned short time_mid;
unsigned short time_hi_and_version;
unsigned char clock_seq_hi_and_reserved;
unsigned char clock_seq_low;
unsigned char node[6];
} efi_guid_t;

if (memcmp(&efiguid, &GUID_ABC, sizeof(efiGuid)) == 0) { <====
error on this line
printf("ABC\n");
} else if (memcmp(&efiguid, &GUID_XYZ, sizeof(efiGuid)) == 0)
{ <=== error on this line
printf("XYZ\n");
} else
printf("unknow\n");
}

Any idea what is the reason the comilation fails on visual c++
compiler.

thanks for any help.
 
G

Gert-Jan de Vos

guys,

  I have a code that has to be compiled with C and C++ compiler both.
WIth C, I don't get any error but with C++, I am getting following
error.

error C2059: syntax error : '{'

Here is the code:
--------------------------------------------------------

#include <stdio.h>
#include <string.h>

typedef unsigned char uint8;
typedef struct {
   uint8 b[16];

} efiGuid;

#define EFI_GUID(a, b, c, d, e0, e1, e2, e3, e4, e5) \
((efiGuid) \
{{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) &
0xff, \
   (b) & 0xff, ((b) >> 8) & 0xff, \
   (c) & 0xff, ((c) >> 8) & 0xff, \
   (d) & 0xff, ((d) >> 8) & 0xff, \
   (e0), (e1), (e2), (e3), (e4), (e5) }})

#define GUID_ABC \
    EFI_GUID(0x00000000, 0x0000, 0x0000, 0x0000, 0x00, 0x00, 0x00,
0x00, 0x00, 0
x00)
#define GUID_XYZ \
    EFI_GUID(0xc12a7328, 0xf81f, 0x11d2, 0xba4b, 0x00, 0xa0, 0xc9,
0x3e, 0xc9, 0
x3b)

Your EFI_GUID is neither valid C nor C++. I guess it relies an a gcc
extension. You can make it work for both C and C++ like this:

#define EFI_GUID(a, b, c, d, e0, e1, e2, e3, e4, e5) \
{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) &
0xff, \
(b) & 0xff, ((b) >> 8) & 0xff, \
(c) & 0xff, ((c) >> 8) & 0xff, \
(d) & 0xff, ((d) >> 8) & 0xff, \
(e0), (e1), (e2), (e3), (e4), (e5) }

efiGuid GUID_ABC = EFI_GUID(0x00000000, 0x0000, 0x0000, 0x0000, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00);
efiGuid GUID_XYZ = EFI_GUID(0xc12a7328, 0xf81f, 0x11d2, 0xba4b, 0x00,
0xa0, 0xc9, 0x3e, 0xc9, 0x3b);

[...]
 
J

junky_fellow

Your EFI_GUID is neither valid C nor C++. I guess it relies an a gcc
extension. You can make it work for both C and C++ like this:

I think its valid C. Plese note that there is an array inside the
structure. That is why, "{{" are required.
In fact, I tried removing '{', still I got the error.
Perhaps it is due to some auto intilaization of array, which c++
doesn't support.

Any idea on how to resolve this.
 
G

Gert-Jan de Vos

I think its valid C. Plese note that there is an array inside the
structure. That is why, "{{" are required.
In fact, I tried removing '{', still I got the error.
Perhaps it is due to some auto intilaization of array, which c++
doesn't support.

Any idea on how to resolve this.

Your &GUID_ABC code expands to:

&((efiGuid) {{ (0x00000000) & 0xf, ((0x00000000) >> 8) &
0xff, ... }} )

You can not make a reference to a compound initializer list block like
that.

My previous example first initializes proper objects of type efiGuid.
Then
you can have a reference to such an object.

This is really a C question, there is nothing specific to C++ in your
code
or your problem.
 
J

junky_fellow

Your &GUID_ABC code expands to:

&((efiGuid) {{ (0x00000000) & 0xf, ((0x00000000) >> 8) &
0xff, ... }} )

You can not make a reference to a compound initializer list block like
that.

My previous example first initializes proper objects of type efiGuid.
Then
you can have a reference to such an object.

You are right. Your previous solution worked fine. Thanks a lot for
your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top