www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - enum in D vs C++

reply "Dan Williams" <dnews ithium.NOSPAM.net> writes:
I'm porting some code from C++ to D for comparison, and I've hit some
head-scratching with enums. Specifically, scope... although I have
appropriately changed the enum definition syntax to match D, I am getting
errors about undefined identifiers when I try to compile. I have searched
the online material and the newsgroups, and found nothing to get me past
this... here is part of the code:

struct a {
    enum b {
        c, d
    };
};

that's a simplified segment. The problem is that later on in the C++ code, I
use c and d, but apparently they are out of scope. I've tried things like
b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit on the
right thing by luck and work the reason out after!) but it still fails to
work.

The C++ code compiles and runs perfectly, by the way.

Is there anything I should know about enums that is not mentioned on the D
site, and that is relevant to this?
Jun 30 2004
next sibling parent reply "Walter" <newshound digitalmars.com> writes:
"Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am getting
 errors about undefined identifiers when I try to compile. I have searched
 the online material and the newsgroups, and found nothing to get me past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the C++ code,
I
 use c and d, but apparently they are out of scope. I've tried things like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit on
the
 right thing by luck and work the reason out after!) but it still fails to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned on the D
 site, and that is relevant to this?
Enums with a tag name are scoped by that tag, so you'd reference the c,d as a.b.c and a.b.d. Enums without a tag name are in the enclosing scope, so it would be a.c and a.d.
Jun 30 2004
parent reply "Dan Williams" <dnews ithium.NOSPAM.net> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:cbutnq$r4s$1 digitaldaemon.com...
 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am
getting
 errors about undefined identifiers when I try to compile. I have
searched
 the online material and the newsgroups, and found nothing to get me past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the C++
code,
 I
 use c and d, but apparently they are out of scope. I've tried things
like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit on
the
 right thing by luck and work the reason out after!) but it still fails
to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned on the
D
 site, and that is relevant to this?
Enums with a tag name are scoped by that tag, so you'd reference the c,d
as
 a.b.c and a.b.d. Enums without a tag name are in the enclosing scope, so
it
 would be a.c and a.d.
Thanks... I think. I'm still having problems. There's a union in the 'a' struct, and the members are addressed in the way that you say - but then, they were anyway. As for the enum... I end up with this code: struct a { enum b { c, d }; }; typedef a var; ... var e; e.b = e.b.c; ... In C++, the code was 'e.b = c' but that gets an error of 'undefined identifier c'. The problem is, the new code not only just looks *wrong*, and *feels* wrong, but it generates another error: 'e.b is not an lvalue', whatever that means.
Jun 30 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
news:cbuugk$s37$1 digitaldaemon.com...
 "Walter" <newshound digitalmars.com> wrote in message
 news:cbutnq$r4s$1 digitaldaemon.com...
 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am
getting
 errors about undefined identifiers when I try to compile. I have
searched
 the online material and the newsgroups, and found nothing to get me
past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the C++
code,
 I
 use c and d, but apparently they are out of scope. I've tried things
like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit
on
 the
 right thing by luck and work the reason out after!) but it still fails
to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned on
the
 D
 site, and that is relevant to this?
Enums with a tag name are scoped by that tag, so you'd reference the c,d
as
 a.b.c and a.b.d. Enums without a tag name are in the enclosing scope, so
it
 would be a.c and a.d.
Thanks... I think. I'm still having problems. There's a union in the 'a' struct, and the members are addressed in the way that you say - but then, they were
anyway.
 As for the enum... I end up with this code:

 struct a {
     enum b {
         c, d
     };
 };

 typedef a var;

 ...

     var e;
     e.b = e.b.c;

 ...

 In C++, the code was 'e.b = c' but that gets an error of 'undefined
 identifier c'. The problem is, the new code not only just looks *wrong*,
and
 *feels* wrong, but it generates another error: 'e.b is not an lvalue',
 whatever that means.
The code *is* wrong. b is the name of the enum, it is not a member variable. Not an lvalue means it isn't a memory location that can be assigned to.
Jun 30 2004
parent "Dan Williams" <dnews ithium.NOSPAM.net> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:cbv8ft$1alf$1 digitaldaemon.com...
 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbuugk$s37$1 digitaldaemon.com...
 "Walter" <newshound digitalmars.com> wrote in message
 news:cbutnq$r4s$1 digitaldaemon.com...
 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit
some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am
getting
 errors about undefined identifiers when I try to compile. I have
searched
 the online material and the newsgroups, and found nothing to get me
past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the C++
code,
 I
 use c and d, but apparently they are out of scope. I've tried things
like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit
on
 the
 right thing by luck and work the reason out after!) but it still
fails
 to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned on
the
 D
 site, and that is relevant to this?
Enums with a tag name are scoped by that tag, so you'd reference the
c,d
 as
 a.b.c and a.b.d. Enums without a tag name are in the enclosing scope,
so
 it
 would be a.c and a.d.
Thanks... I think. I'm still having problems. There's a union in the 'a' struct, and the members are addressed in the way that you say - but then, they were
anyway.
 As for the enum... I end up with this code:

 struct a {
     enum b {
         c, d
     };
 };

 typedef a var;

 ...

     var e;
     e.b = e.b.c;

 ...

 In C++, the code was 'e.b = c' but that gets an error of 'undefined
 identifier c'. The problem is, the new code not only just looks *wrong*,
and
 *feels* wrong, but it generates another error: 'e.b is not an lvalue',
 whatever that means.
The code *is* wrong. b is the name of the enum, it is not a member
variable.
 Not an lvalue means it isn't a memory location that can be assigned to.
Got it solved in the end, thanks to Ben Hinkle... I simply needed to instantiate the enum: struct a { enum b_t { c, d } b_t b; } typedef a var; ... var e; e.b = e.b.c; ... The e.b = e.b.c bit was actually ok in the end!
Jun 30 2004
prev sibling next sibling parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
works for me:

struct a {
  enum b {
    c, d
  }
  static void foo() { printf("%d\n",b.d); }
}

int main() {
  printf("%d\n", a.b.d);
  a.foo();
  return 0;
}

I guess more details about your code would be needed to figure out what is
going wrong for you.

"Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am getting
 errors about undefined identifiers when I try to compile. I have searched
 the online material and the newsgroups, and found nothing to get me past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the C++ code,
I
 use c and d, but apparently they are out of scope. I've tried things like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit on
the
 right thing by luck and work the reason out after!) but it still fails to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned on the D
 site, and that is relevant to this?
Jun 30 2004
next sibling parent "Dan Williams" <dnews ithium.NOSPAM.net> writes:
Hmmm... indeed your code does work fine.

I'm gonna go back to the drawing board with this one; if I don't manage to
fix it then I'll probably have to post more code.



"Ben Hinkle" <bhinkle mathworks.com> wrote in message
news:cbuttk$rau$1 digitaldaemon.com...
 works for me:

 struct a {
   enum b {
     c, d
   }
   static void foo() { printf("%d\n",b.d); }
 }

 int main() {
   printf("%d\n", a.b.d);
   a.foo();
   return 0;
 }

 I guess more details about your code would be needed to figure out what is
 going wrong for you.

 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am
getting
 errors about undefined identifiers when I try to compile. I have
searched
 the online material and the newsgroups, and found nothing to get me past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the C++
code,
 I
 use c and d, but apparently they are out of scope. I've tried things
like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit on
the
 right thing by luck and work the reason out after!) but it still fails
to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned on the
D
 site, and that is relevant to this?
Jun 30 2004
prev sibling parent reply "Dan Williams" <dnews ithium.NOSPAM.net> writes:
Ok, I am now quite confused, which is not good, because surely there is not
much about enums that I should be getting confused about! They are, after
all, just a simple data type.

I expanded your code to make it more like mine until I encountered the error
again. This is the code:

struct a {
  enum b {
    c, d
  }
}
typedef a Z;
int main() {
  Z e;
  e.b = e.b.d;
  printf("%d\n", e.b);
  return 0;
}

...it fails with that error that I mentioned in a previous message: "e.b. is
not an lvalue". Which is weird, because I assume lvalue simply means an
assignable entity, and it certainly should be assignable? In the sense that
I should be able to assign a value to it? Maybe that message means something
else.

Anyway, before posting I decided to make the code a bit more meaningful by
giving different variable names. This is the altered code:

struct vehicle {
  enum body {
    saloon, suv
  }
}
typedef vehicle car;
int main() {
  car myCar;
  myCar.body = myCar.body.suv;
  printf("%d\n", myCar.body);
  return 0;
}

Now, I am pretty sure that every entity there is semantically the same, just
changed in name from 'a' to 'vehicle' for instance. But bizarrely, I get
these errors:

line 6: { enum members } expected
line 6: Declaration expected, not 'body'
line 13: identifier expected following '.', not 'body'
line 13: identifier expected following '.', not 'body'
line 14: identifier expected following '.', not 'body'
line 17: struct member expected

Gah! Very weird!

Just so that you can compare, I ported the above car example BACK to C++.
And guess what - it compiled and ran fine. Here it is:

struct vehicle {
 enum {
  saloon, suv
 } body;
};
typedef struct vehicle car;
int main() {
 car myCar;
 myCar.body = suv;
 printf("%d\n", myCar.body);
 return 0;
}

Hopefully that will give you enough to go on to work out what on earth the
problem is...



"Ben Hinkle" <bhinkle mathworks.com> wrote in message
news:cbuttk$rau$1 digitaldaemon.com...
 works for me:

 struct a {
   enum b {
     c, d
   }
   static void foo() { printf("%d\n",b.d); }
 }

 int main() {
   printf("%d\n", a.b.d);
   a.foo();
   return 0;
 }

 I guess more details about your code would be needed to figure out what is
 going wrong for you.

 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am
getting
 errors about undefined identifiers when I try to compile. I have
searched
 the online material and the newsgroups, and found nothing to get me past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the C++
code,
 I
 use c and d, but apparently they are out of scope. I've tried things
like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit on
the
 right thing by luck and work the reason out after!) but it still fails
to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned on the
D
 site, and that is relevant to this?
Jun 30 2004
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
news:cbv2l4$1277$1 digitaldaemon.com...
 Ok, I am now quite confused, which is not good, because surely there is
not
 much about enums that I should be getting confused about! They are, after
 all, just a simple data type.

 I expanded your code to make it more like mine until I encountered the
error
 again. This is the code:

 struct a {
   enum b {
     c, d
   }
 }
 typedef a Z;
 int main() {
   Z e;
   e.b = e.b.d;
   printf("%d\n", e.b);
   return 0;
 }

 ...it fails with that error that I mentioned in a previous message: "e.b.
is
 not an lvalue". Which is weird, because I assume lvalue simply means an
 assignable entity, and it certainly should be assignable? In the sense
that
 I should be able to assign a value to it? Maybe that message means
something
 else.
e.b is not an lvalue. "b" is a type name not a field name. Try something like struct vehicle { enum body_t { saloon, suv } body_t body; }
 Anyway, before posting I decided to make the code a bit more meaningful by
 giving different variable names. This is the altered code:

 struct vehicle {
   enum body {
     saloon, suv
   }
 }
 typedef vehicle car;
 int main() {
   car myCar;
   myCar.body = myCar.body.suv;
   printf("%d\n", myCar.body);
   return 0;
 }

 Now, I am pretty sure that every entity there is semantically the same,
just
 changed in name from 'a' to 'vehicle' for instance. But bizarrely, I get
 these errors:

 line 6: { enum members } expected
 line 6: Declaration expected, not 'body'
 line 13: identifier expected following '.', not 'body'
 line 13: identifier expected following '.', not 'body'
 line 14: identifier expected following '.', not 'body'
 line 17: struct member expected

 Gah! Very weird!

 Just so that you can compare, I ported the above car example BACK to C++.
 And guess what - it compiled and ran fine. Here it is:

 struct vehicle {
  enum {
   saloon, suv
  } body;
 };
 typedef struct vehicle car;
 int main() {
  car myCar;
  myCar.body = suv;
  printf("%d\n", myCar.body);
  return 0;
 }

 Hopefully that will give you enough to go on to work out what on earth the
 problem is...



 "Ben Hinkle" <bhinkle mathworks.com> wrote in message
 news:cbuttk$rau$1 digitaldaemon.com...
 works for me:

 struct a {
   enum b {
     c, d
   }
   static void foo() { printf("%d\n",b.d); }
 }

 int main() {
   printf("%d\n", a.b.d);
   a.foo();
   return 0;
 }

 I guess more details about your code would be needed to figure out what
is
 going wrong for you.

 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am
getting
 errors about undefined identifiers when I try to compile. I have
searched
 the online material and the newsgroups, and found nothing to get me
past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the C++
code,
 I
 use c and d, but apparently they are out of scope. I've tried things
like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit
on
 the
 right thing by luck and work the reason out after!) but it still fails
to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned on
the
 D
 site, and that is relevant to this?
Jun 30 2004
parent reply "Dan Williams" <dnews ithium.NOSPAM.net> writes:
w00t! Thankyou for that... I made the change you suggested, and it worked :)

Looking at the enum docs, I don't think this is really made very clear. The
only mention of this is right at the bottom of the page, and it doesn't
stand out as something I had to do.

Well, I was then able to find that despite the first example then working,
the second did not. I changed 'body' to 'shape' and it ran just fine.
Unfortunate choice of word, then... I figured body was a keyword and managed
to locate a list at http://www.digitalmars.com/d/lex.html

Well, thankyou, stupid problem that, but I'm glad to have it solved :)




"Ben Hinkle" <bhinkle mathworks.com> wrote in message
news:cbv3d4$13eb$1 digitaldaemon.com...
 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbv2l4$1277$1 digitaldaemon.com...
 Ok, I am now quite confused, which is not good, because surely there is
not
 much about enums that I should be getting confused about! They are,
after
 all, just a simple data type.

 I expanded your code to make it more like mine until I encountered the
error
 again. This is the code:

 struct a {
   enum b {
     c, d
   }
 }
 typedef a Z;
 int main() {
   Z e;
   e.b = e.b.d;
   printf("%d\n", e.b);
   return 0;
 }

 ...it fails with that error that I mentioned in a previous message:
"e.b.
 is
 not an lvalue". Which is weird, because I assume lvalue simply means an
 assignable entity, and it certainly should be assignable? In the sense
that
 I should be able to assign a value to it? Maybe that message means
something
 else.
e.b is not an lvalue. "b" is a type name not a field name. Try something like struct vehicle { enum body_t { saloon, suv } body_t body; }
 Anyway, before posting I decided to make the code a bit more meaningful
by
 giving different variable names. This is the altered code:

 struct vehicle {
   enum body {
     saloon, suv
   }
 }
 typedef vehicle car;
 int main() {
   car myCar;
   myCar.body = myCar.body.suv;
   printf("%d\n", myCar.body);
   return 0;
 }

 Now, I am pretty sure that every entity there is semantically the same,
just
 changed in name from 'a' to 'vehicle' for instance. But bizarrely, I get
 these errors:

 line 6: { enum members } expected
 line 6: Declaration expected, not 'body'
 line 13: identifier expected following '.', not 'body'
 line 13: identifier expected following '.', not 'body'
 line 14: identifier expected following '.', not 'body'
 line 17: struct member expected

 Gah! Very weird!

 Just so that you can compare, I ported the above car example BACK to
C++.
 And guess what - it compiled and ran fine. Here it is:

 struct vehicle {
  enum {
   saloon, suv
  } body;
 };
 typedef struct vehicle car;
 int main() {
  car myCar;
  myCar.body = suv;
  printf("%d\n", myCar.body);
  return 0;
 }

 Hopefully that will give you enough to go on to work out what on earth
the
 problem is...



 "Ben Hinkle" <bhinkle mathworks.com> wrote in message
 news:cbuttk$rau$1 digitaldaemon.com...
 works for me:

 struct a {
   enum b {
     c, d
   }
   static void foo() { printf("%d\n",b.d); }
 }

 int main() {
   printf("%d\n", a.b.d);
   a.foo();
   return 0;
 }

 I guess more details about your code would be needed to figure out
what
 is
 going wrong for you.

 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit
some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am
getting
 errors about undefined identifiers when I try to compile. I have
searched
 the online material and the newsgroups, and found nothing to get me
past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the C++
code,
 I
 use c and d, but apparently they are out of scope. I've tried things
like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit
on
 the
 right thing by luck and work the reason out after!) but it still
fails
 to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned on
the
 D
 site, and that is relevant to this?
Jun 30 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Wed, 30 Jun 2004 20:31:00 +0100, Dan Williams <dnews ithium.NOSPAM.net> 
wrote:
 w00t! Thankyou for that... I made the change you suggested, and it 
 worked :)

 Looking at the enum docs, I don't think this is really made very clear. 
 The
 only mention of this is right at the bottom of the page, and it doesn't
 stand out as something I had to do.

 Well, I was then able to find that despite the first example then 
 working,
 the second did not. I changed 'body' to 'shape' and it ran just fine.
 Unfortunate choice of word, then... I figured body was a keyword and 
 managed
 to locate a list at http://www.digitalmars.com/d/lex.html

 Well, thankyou, stupid problem that, but I'm glad to have it solved :)
FYI.. I think the reason this had you confused is that your C code.. struct vehicle { enum { saloon, suv } body; }; does not mean what you think it means. The above defines an *unnamed* enum AND declares an instance of it in the struct. The D Ben posted... struct vehicle { enum body_t { //defines an enum called 'body_t' saloon, suv } body_t body; //creates an instance of it. } is a complete/correct translation of the C, whereas your translation... struct a { enum b { //defines an enum called 'b' c, d } } does not create an instance of the defined enum in your struct, which is not possible in D IIRC. The D documentation should probably outline this fact. Regan.
 "Ben Hinkle" <bhinkle mathworks.com> wrote in message
 news:cbv3d4$13eb$1 digitaldaemon.com...
 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbv2l4$1277$1 digitaldaemon.com...
 Ok, I am now quite confused, which is not good, because surely there 
is not
 much about enums that I should be getting confused about! They are,
after
 all, just a simple data type.

 I expanded your code to make it more like mine until I encountered the
error
 again. This is the code:

 struct a {
   enum b {
     c, d
   }
 }
 typedef a Z;
 int main() {
   Z e;
   e.b = e.b.d;
   printf("%d\n", e.b);
   return 0;
 }

 ...it fails with that error that I mentioned in a previous message:
"e.b.
 is
 not an lvalue". Which is weird, because I assume lvalue simply means 
an
 assignable entity, and it certainly should be assignable? In the sense
that
 I should be able to assign a value to it? Maybe that message means
something
 else.
e.b is not an lvalue. "b" is a type name not a field name. Try something like struct vehicle { enum body_t { saloon, suv } body_t body; }
 Anyway, before posting I decided to make the code a bit more 
meaningful
by
 giving different variable names. This is the altered code:

 struct vehicle {
   enum body {
     saloon, suv
   }
 }
 typedef vehicle car;
 int main() {
   car myCar;
   myCar.body = myCar.body.suv;
   printf("%d\n", myCar.body);
   return 0;
 }

 Now, I am pretty sure that every entity there is semantically the 
same, just
 changed in name from 'a' to 'vehicle' for instance. But bizarrely, I 
get
 these errors:

 line 6: { enum members } expected
 line 6: Declaration expected, not 'body'
 line 13: identifier expected following '.', not 'body'
 line 13: identifier expected following '.', not 'body'
 line 14: identifier expected following '.', not 'body'
 line 17: struct member expected

 Gah! Very weird!

 Just so that you can compare, I ported the above car example BACK to
C++.
 And guess what - it compiled and ran fine. Here it is:

 struct vehicle {
  enum {
   saloon, suv
  } body;
 };
 typedef struct vehicle car;
 int main() {
  car myCar;
  myCar.body = suv;
  printf("%d\n", myCar.body);
  return 0;
 }

 Hopefully that will give you enough to go on to work out what on earth
the
 problem is...



 "Ben Hinkle" <bhinkle mathworks.com> wrote in message
 news:cbuttk$rau$1 digitaldaemon.com...
 works for me:

 struct a {
   enum b {
     c, d
   }
   static void foo() { printf("%d\n",b.d); }
 }

 int main() {
   printf("%d\n", a.b.d);
   a.foo();
   return 0;
 }

 I guess more details about your code would be needed to figure out
what
 is
 going wrong for you.

 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit
some
 head-scratching with enums. Specifically, scope... although I have
 appropriately changed the enum definition syntax to match D, I am
getting
 errors about undefined identifiers when I try to compile. I have
searched
 the online material and the newsgroups, and found nothing to get 
me past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the 
C++
 code,
 I
 use c and d, but apparently they are out of scope. I've tried 
things
 like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I 
hit on
 the
 right thing by luck and work the reason out after!) but it still
fails
 to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned 
on the
 D
 site, and that is relevant to this?
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 30 2004
next sibling parent reply "Walter" <newshound digitalmars.com> writes:
"Regan Heath" <regan netwin.co.nz> wrote in message
news:opsafgufky5a2sq9 digitalmars.com...
 FYI.. I think the reason this had you confused is that your C code..

 struct vehicle {
   enum {
    saloon, suv
   } body;
 };

 does not mean what you think it means. The above defines an *unnamed* enum
 AND declares an instance of it in the struct. The D Ben posted...

 struct vehicle {
   enum body_t {  //defines an enum called 'body_t'
    saloon, suv
   }
   body_t body;   //creates an instance of it.
 }

 is a complete/correct translation of the C, whereas your translation...

 struct a {
    enum b {  //defines an enum called 'b'
      c, d
    }
 }

 does not create an instance of the defined enum in your struct, which is
 not possible in D IIRC. The D documentation should probably outline this
 fact.
Sure it's possible: struct a { enum b { c, d } b e; // e is an instance of enum b }
Jun 30 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Wed, 30 Jun 2004 15:04:27 -0700, Walter <newshound digitalmars.com> 
wrote:

 "Regan Heath" <regan netwin.co.nz> wrote in message
 news:opsafgufky5a2sq9 digitalmars.com...
 FYI.. I think the reason this had you confused is that your C code..

 struct vehicle {
   enum {
    saloon, suv
   } body;
 };

 does not mean what you think it means. The above defines an *unnamed* 
 enum
 AND declares an instance of it in the struct. The D Ben posted...

 struct vehicle {
   enum body_t {  //defines an enum called 'body_t'
    saloon, suv
   }
   body_t body;   //creates an instance of it.
 }

 is a complete/correct translation of the C, whereas your translation...

 struct a {
    enum b {  //defines an enum called 'b'
      c, d
    }
 }

 does not create an instance of the defined enum in your struct, which is
 not possible in D IIRC. The D documentation should probably outline this
 fact.
Sure it's possible: struct a { enum b { c, d } b e; // e is an instance of enum b }
but this is not? struct a { enum b { c,d } e; } correct? that is what I mean't was not possible, I phrased it badly. Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 30 2004
parent "Walter" <newshound digitalmars.com> writes:
"Regan Heath" <regan netwin.co.nz> wrote in message
news:opsaflv6t15a2sq9 digitalmars.com...
 Sure it's possible:

 struct a {
     enum b { c, d }
     b e;    // e is an instance of enum b
 }
but this is not? struct a { enum b { c,d } e; } correct?
Yes, that won't work.
 that is what I mean't was not possible, I phrased it badly.

 Regan.

 -- 
 Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 30 2004
prev sibling parent "Dan Williams" <dnews ithium.NOSPAM.net> writes:
Indeed, many things I have to learn about D :)


"Regan Heath" <regan netwin.co.nz> wrote in message
news:opsafgufky5a2sq9 digitalmars.com...
 On Wed, 30 Jun 2004 20:31:00 +0100, Dan Williams <dnews ithium.NOSPAM.net>
 wrote:
 w00t! Thankyou for that... I made the change you suggested, and it
 worked :)

 Looking at the enum docs, I don't think this is really made very clear.
 The
 only mention of this is right at the bottom of the page, and it doesn't
 stand out as something I had to do.

 Well, I was then able to find that despite the first example then
 working,
 the second did not. I changed 'body' to 'shape' and it ran just fine.
 Unfortunate choice of word, then... I figured body was a keyword and
 managed
 to locate a list at http://www.digitalmars.com/d/lex.html

 Well, thankyou, stupid problem that, but I'm glad to have it solved :)
FYI.. I think the reason this had you confused is that your C code.. struct vehicle { enum { saloon, suv } body; }; does not mean what you think it means. The above defines an *unnamed* enum AND declares an instance of it in the struct. The D Ben posted... struct vehicle { enum body_t { //defines an enum called 'body_t' saloon, suv } body_t body; //creates an instance of it. } is a complete/correct translation of the C, whereas your translation... struct a { enum b { //defines an enum called 'b' c, d } } does not create an instance of the defined enum in your struct, which is not possible in D IIRC. The D documentation should probably outline this fact. Regan.
 "Ben Hinkle" <bhinkle mathworks.com> wrote in message
 news:cbv3d4$13eb$1 digitaldaemon.com...
 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbv2l4$1277$1 digitaldaemon.com...
 Ok, I am now quite confused, which is not good, because surely there
is not
 much about enums that I should be getting confused about! They are,
after
 all, just a simple data type.

 I expanded your code to make it more like mine until I encountered
the
 error
 again. This is the code:

 struct a {
   enum b {
     c, d
   }
 }
 typedef a Z;
 int main() {
   Z e;
   e.b = e.b.d;
   printf("%d\n", e.b);
   return 0;
 }

 ...it fails with that error that I mentioned in a previous message:
"e.b.
 is
 not an lvalue". Which is weird, because I assume lvalue simply means
an
 assignable entity, and it certainly should be assignable? In the
sense
 that
 I should be able to assign a value to it? Maybe that message means
something
 else.
e.b is not an lvalue. "b" is a type name not a field name. Try
something
 like

 struct vehicle {
    enum body_t {
      saloon, suv
    }
    body_t body;
  }

 Anyway, before posting I decided to make the code a bit more
meaningful
by
 giving different variable names. This is the altered code:

 struct vehicle {
   enum body {
     saloon, suv
   }
 }
 typedef vehicle car;
 int main() {
   car myCar;
   myCar.body = myCar.body.suv;
   printf("%d\n", myCar.body);
   return 0;
 }

 Now, I am pretty sure that every entity there is semantically the
same, just
 changed in name from 'a' to 'vehicle' for instance. But bizarrely, I
get
 these errors:

 line 6: { enum members } expected
 line 6: Declaration expected, not 'body'
 line 13: identifier expected following '.', not 'body'
 line 13: identifier expected following '.', not 'body'
 line 14: identifier expected following '.', not 'body'
 line 17: struct member expected

 Gah! Very weird!

 Just so that you can compare, I ported the above car example BACK to
C++.
 And guess what - it compiled and ran fine. Here it is:

 struct vehicle {
  enum {
   saloon, suv
  } body;
 };
 typedef struct vehicle car;
 int main() {
  car myCar;
  myCar.body = suv;
  printf("%d\n", myCar.body);
  return 0;
 }

 Hopefully that will give you enough to go on to work out what on
earth
 the
 problem is...



 "Ben Hinkle" <bhinkle mathworks.com> wrote in message
 news:cbuttk$rau$1 digitaldaemon.com...
 works for me:

 struct a {
   enum b {
     c, d
   }
   static void foo() { printf("%d\n",b.d); }
 }

 int main() {
   printf("%d\n", a.b.d);
   a.foo();
   return 0;
 }

 I guess more details about your code would be needed to figure out
what
 is
 going wrong for you.

 "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message
 news:cbus74$p04$1 digitaldaemon.com...
 I'm porting some code from C++ to D for comparison, and I've hit
some
 head-scratching with enums. Specifically, scope... although I
have
 appropriately changed the enum definition syntax to match D, I am
getting
 errors about undefined identifiers when I try to compile. I have
searched
 the online material and the newsgroups, and found nothing to get
me past
 this... here is part of the code:

 struct a {
     enum b {
         c, d
     };
 };

 that's a simplified segment. The problem is that later on in the
C++
 code,
 I
 use c and d, but apparently they are out of scope. I've tried
things
 like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I
hit on
 the
 right thing by luck and work the reason out after!) but it still
fails
 to
 work.

 The C++ code compiles and runs perfectly, by the way.

 Is there anything I should know about enums that is not mentioned
on the
 D
 site, and that is relevant to this?
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Jun 30 2004
prev sibling parent "Lynn Allan" <l.allan att.net> writes:
I've tried things like
 b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit on
the
 right thing by luck and work the reason out after!) but it still fails to
 work.
Hi Dan et'al, Ever heard of a "gub"? It's "b.u.g" backwards = "g.u.b" Bug ... doesn't work and don't know why Gug ... works, but don't know why :-D Lynn A.
Jun 30 2004