digitalmars.D - enum in D vs C++
- Dan Williams (19/19) Jun 30 2004 I'm porting some code from C++ to D for comparison, and I've hit some
- Walter (7/26) Jun 30 2004 I
- Dan Williams (28/58) Jun 30 2004 getting
- Walter (9/71) Jun 30 2004 past
- Dan Williams (21/100) Jun 30 2004 some
- Ben Hinkle (18/37) Jun 30 2004 works for me:
- Dan Williams (11/55) Jun 30 2004 Hmmm... indeed your code does work fine.
- Dan Williams (70/114) Jun 30 2004 Ok, I am now quite confused, which is not good, because surely there is ...
- Ben Hinkle (20/140) Jun 30 2004 not
- Dan Williams (19/175) Jun 30 2004 w00t! Thankyou for that... I made the change you suggested, and it worke...
- Regan Heath (28/223) Jun 30 2004 FYI.. I think the reason this had you confused is that your C code..
- Walter (7/30) Jun 30 2004 Sure it's possible:
- Regan Heath (11/48) Jun 30 2004 but this is not?
- Walter (3/18) Jun 30 2004 Yes, that won't work.
- Dan Williams (8/236) Jun 30 2004 Indeed, many things I have to learn about D :)
- Lynn Allan (8/12) Jun 30 2004 Hi Dan et'al,
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
"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,Iuse 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 ontheright 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
"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...gettingI'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 amsearchederrors about undefined identifiers when I try to compile. I havecode,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++Ilikeuse c and d, but apparently they are out of scope. I've tried thingstob.c etc. and a.b.c (without actually knowing *why* - sometimes I hit ontheright thing by luck and work the reason out after!) but it still failsDwork. The C++ code compiles and runs perfectly, by the way. Is there anything I should know about enums that is not mentioned on theassite, and that is relevant to this?Enums with a tag name are scoped by that tag, so you'd reference the c,da.b.c and a.b.d. Enums without a tag name are in the enclosing scope, soitwould 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
"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...past"Dan Williams" <dnews ithium.NOSPAM.net> wrote in message news:cbus74$p04$1 digitaldaemon.com...gettingI'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 amsearchederrors about undefined identifiers when I try to compile. I havethe online material and the newsgroups, and found nothing to get meoncode,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++Ilikeuse c and d, but apparently they are out of scope. I've tried thingsb.c etc. and a.b.c (without actually knowing *why* - sometimes I hitthethetoright thing by luck and work the reason out after!) but it still failswork. The C++ code compiles and runs perfectly, by the way. Is there anything I should know about enums that is not mentioned onDanyway.assite, and that is relevant to this?Enums with a tag name are scoped by that tag, so you'd reference the c,da.b.c and a.b.d. Enums without a tag name are in the enclosing scope, soitwould 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 wereAs 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
"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...some"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 hitfailspastgettinghead-scratching with enums. Specifically, scope... although I have appropriately changed the enum definition syntax to match D, I amsearchederrors about undefined identifiers when I try to compile. I havethe online material and the newsgroups, and found nothing to get meoncode,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++Ilikeuse c and d, but apparently they are out of scope. I've tried thingsb.c etc. and a.b.c (without actually knowing *why* - sometimes I hittheright thing by luck and work the reason out after!) but it stillc,dtothework. The C++ code compiles and runs perfectly, by the way. Is there anything I should know about enums that is not mentioned onDsite, and that is relevant to this?Enums with a tag name are scoped by that tag, so you'd reference thesoasa.b.c and a.b.d. Enums without a tag name are in the enclosing scope,variable.itanyway.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 wereAs 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 memberNot 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
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,Iuse 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 ontheright 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
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...gettingI'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 amsearchederrors about undefined identifiers when I try to compile. I havecode,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++Ilikeuse c and d, but apparently they are out of scope. I've tried thingstob.c etc. and a.b.c (without actually knowing *why* - sometimes I hit ontheright thing by luck and work the reason out after!) but it still failsDwork. The C++ code compiles and runs perfectly, by the way. Is there anything I should know about enums that is not mentioned on thesite, and that is relevant to this?
Jun 30 2004
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...gettingI'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 amsearchederrors about undefined identifiers when I try to compile. I havecode,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++Ilikeuse c and d, but apparently they are out of scope. I've tried thingstob.c etc. and a.b.c (without actually knowing *why* - sometimes I hit ontheright thing by luck and work the reason out after!) but it still failsDwork. The C++ code compiles and runs perfectly, by the way. Is there anything I should know about enums that is not mentioned on thesite, and that is relevant to this?
Jun 30 2004
"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 isnotmuch 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 theerroragain. 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.isnot an lvalue". Which is weird, because I assume lvalue simply means an assignable entity, and it certainly should be assignable? In the sensethatI should be able to assign a value to it? Maybe that message meanssomethingelse.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,justchanged 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...isworks 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 whatpastgoing wrong for you. "Dan Williams" <dnews ithium.NOSPAM.net> wrote in message news:cbus74$p04$1 digitaldaemon.com...gettingI'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 amsearchederrors about undefined identifiers when I try to compile. I havethe online material and the newsgroups, and found nothing to get meoncode,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++Ilikeuse c and d, but apparently they are out of scope. I've tried thingsb.c etc. and a.b.c (without actually knowing *why* - sometimes I hitthethetoright thing by luck and work the reason out after!) but it still failswork. The C++ code compiles and runs perfectly, by the way. Is there anything I should know about enums that is not mentioned onDsite, and that is relevant to this?
Jun 30 2004
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...afterOk, I am now quite confused, which is not good, because surely there isnotmuch about enums that I should be getting confused about! They are,"e.b.all, just a simple data type. I expanded your code to make it more like mine until I encountered theerroragain. 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:isbynot an lvalue". Which is weird, because I assume lvalue simply means an assignable entity, and it certainly should be assignable? In the sensethatI should be able to assign a value to it? Maybe that message meanssomethingelse.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 meaningfulC++.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,justchanged 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 totheAnd 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 earthwhatproblem 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 outissomegoing 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 hitfailspastgettinghead-scratching with enums. Specifically, scope... although I have appropriately changed the enum definition syntax to match D, I amsearchederrors about undefined identifiers when I try to compile. I havethe online material and the newsgroups, and found nothing to get meoncode,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++Ilikeuse c and d, but apparently they are out of scope. I've tried thingsb.c etc. and a.b.c (without actually knowing *why* - sometimes I hittheright thing by luck and work the reason out after!) but it stilltothework. The C++ code compiles and runs perfectly, by the way. Is there anything I should know about enums that is not mentioned onDsite, and that is relevant to this?
Jun 30 2004
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...-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/"Dan Williams" <dnews ithium.NOSPAM.net> wrote in message news:cbv2l4$1277$1 digitaldaemon.com...afterOk, I am now quite confused, which is not good, because surely thereis notmuch about enums that I should be getting confused about! They are,"e.b.all, just a simple data type. I expanded your code to make it more like mine until I encountered theerroragain. 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:isbynot an lvalue". Which is weird, because I assume lvalue simply meansanassignable entity, and it certainly should be assignable? In the sensethatI should be able to assign a value to it? Maybe that message meanssomethingelse.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 moremeaningfulC++.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 thesame, justchanged in name from 'a' to 'vehicle' for instance. But bizarrely, Igetthese 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 totheAnd 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 earthwhatproblem 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 outissomegoing 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 hitfailsme pastgettinghead-scratching with enums. Specifically, scope... although I have appropriately changed the enum definition syntax to match D, I amsearchederrors about undefined identifiers when I try to compile. I havethe online material and the newsgroups, and found nothing to getC++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 thecode,thingsIuse c and d, but apparently they are out of scope. I've triedlikehit onb.c etc. and a.b.c (without actually knowing *why* - sometimes Itheright thing by luck and work the reason out after!) but it stilltoon thework. The C++ code compiles and runs perfectly, by the way. Is there anything I should know about enums that is not mentionedDsite, and that is relevant to this?
Jun 30 2004
"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
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...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/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
"Regan Heath" <regan netwin.co.nz> wrote in message news:opsaflv6t15a2sq9 digitalmars.com...Yes, that won't work.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
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:thew00t! 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...afterOk, I am now quite confused, which is not good, because surely thereis notmuch about enums that I should be getting confused about! They are,all, just a simple data type. I expanded your code to make it more like mine until I encounteredsenseerror"e.b.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:isnot an lvalue". Which is weird, because I assume lvalue simply meansanassignable entity, and it certainly should be assignable? In thesomethingthatI should be able to assign a value to it? Maybe that message meanssomethingelse.e.b is not an lvalue. "b" is a type name not a field name. Tryearthlike struct vehicle { enum body_t { saloon, suv } body_t body; }byAnyway, before posting I decided to make the code a bit moremeaningfulC++.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 thesame, justchanged in name from 'a' to 'vehicle' for instance. But bizarrely, Igetthese 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 toAnd 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 onhavethewhatproblem 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 outissomegoing 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 hithead-scratching with enums. Specifically, scope... although I-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/failsme pastgettingappropriately changed the enum definition syntax to match D, I amsearchederrors about undefined identifiers when I try to compile. I havethe online material and the newsgroups, and found nothing to getC++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 thecode,thingsIuse c and d, but apparently they are out of scope. I've triedlikehit onb.c etc. and a.b.c (without actually knowing *why* - sometimes Itheright thing by luck and work the reason out after!) but it stilltoon thework. The C++ code compiles and runs perfectly, by the way. Is there anything I should know about enums that is not mentionedDsite, and that is relevant to this?
Jun 30 2004
I've tried things like b.c etc. and a.b.c (without actually knowing *why* - sometimes I hit ontheright 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