Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > newbie c++ question

Reply
Thread Tools

newbie c++ question

 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      09-04-2009
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) >> & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) &
0xff, \
(b) & 0xff, ((b) >> & 0xff, \
(c) & 0xff, ((c) >> & 0xff, \
(d) & 0xff, ((d) >> & 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.
 
Reply With Quote
 
 
 
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      09-04-2009
On Sep 4, 8:54*am, "(E-Mail Removed)"
<(E-Mail Removed)> 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) >> & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) &
> 0xff, \
> * *(b) & 0xff, ((b) >> & 0xff, \
> * *(c) & 0xff, ((c) >> & 0xff, \
> * *(d) & 0xff, ((d) >> & 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) >> & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) &
0xff, \
(b) & 0xff, ((b) >> & 0xff, \
(c) & 0xff, ((c) >> & 0xff, \
(d) & 0xff, ((d) >> & 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.


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-04-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> guys,
>
> I have a code that has to be compiled with C and C++ compiler both.


Why?

--
Ian Collins
 
Reply With Quote
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      09-04-2009
> 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) >> & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) &
> 0xff, \
> * (b) & 0xff, ((b) >> & 0xff, \
> * (c) & 0xff, ((c) >> & 0xff, \
> * (d) & 0xff, ((d) >> & 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);
>

 
Reply With Quote
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      09-04-2009
On Sep 4, 11:50*am, "(E-Mail Removed)"
<(E-Mail Removed)> 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) >> &
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.
 
Reply With Quote
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      09-04-2009
On Sep 4, 3:01*pm, Gert-Jan de Vos <gert-
(E-Mail Removed)> wrote:
> On Sep 4, 11:50*am, "(E-Mail Removed)"
>
> <(E-Mail Removed)> 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) >> &
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VONAGE Newbie w/newbie question New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM



Advertisments