newbie c++ question

Discussion in 'C++' started by junky_fellow@yahoo.co.in, Sep 4, 2009.

  1. Guest

    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.
    , Sep 4, 2009
    #1
    1. Advertising

  2. On Sep 4, 8:54 am, ""
    <> wrote:
    > 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);

    [...]

    > Any idea what is the reason the comilation fails on visual c++
    > compiler.
    Gert-Jan de Vos, Sep 4, 2009
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    > guys,
    >
    > I have a code that has to be compiled with C and C++ compiler both.


    Why?

    --
    Ian Collins
    Ian Collins, Sep 4, 2009
    #3
  4. Guest

    > 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.

    >
    > #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);
    >
    , Sep 4, 2009
    #4
  5. On Sep 4, 11:50 am, ""
    <> wrote:
    > > 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.


    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.
    Gert-Jan de Vos, Sep 4, 2009
    #5
  6. Guest

    On Sep 4, 3:01 pm, Gert-Jan de Vos <gert-
    > wrote:
    > On Sep 4, 11:50 am, ""
    >
    > <> wrote:
    > > > 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.

    >
    > 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.
    , Sep 4, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry C.
    Replies:
    8
    Views:
    229
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Replies:
    0
    Views:
    135
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Replies:
    0
    Views:
    118
    Dave Angel
    Apr 7, 2013
  4. rusi
    Replies:
    0
    Views:
    108
  5. Miki Tebeka
    Replies:
    0
    Views:
    83
    Miki Tebeka
    Apr 7, 2013
Loading...

Share This Page