www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - named union : needs 'this' to access member error

reply David L. Davis <SpottedTiger yahoo.com> writes:































Output:
----------
C:\dmd>dmd needthis.d
needthis.d(26): need 'this' to access member AsciiChar

C:\dmd>

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Jun 17 2005
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Sat, 18 Jun 2005 02:24:55 +0000 (UTC), David L. Davis wrote:































 
 Output:
 ----------
 C:\dmd>dmd needthis.d
 needthis.d(26): need 'this' to access member AsciiChar
 
 C:\dmd>
Not a bug. If you name a union, then you must create an instance of it before you can reference its members. If you don't name a union, then the union becomes its own instance. struct CHAR_INFO { union { WCHAR UnicodeChar; CHAR AsciiChar; } } In this case, the anonymous union is also an instance of itself so you can reference its members directly. CHAR_INFO chiFill; chiFill.UnicodeChar = '\x20'; However ... struct CHAR_INFO { union Char { WCHAR UnicodeChar; CHAR AsciiChar; } Char aChar; } In this case, the union is named so you have to create an instance of it first, then reference member through that instance. CHAR_INFO chiFill; chiFill.aChar.UnicodeChar = '\x20'; If you use the union name itself in the reference, then you need to only reference 'static' members, and I'm not sure what that would be. -- Derek Melbourne, Australia 18/06/2005 12:46:21 PM
Jun 17 2005
next sibling parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Damn, Derek was faster ;)


-- 
Tomasz Stachowiak  /+ a.k.a. h3r3tic +/
Jun 17 2005
prev sibling parent David L. Davis <SpottedTiger yahoo.com> writes:
In article <trarz7rthih8.140fc8415dmrw$.dlg 40tude.net>, Derek Parnell says...
Not a bug.

If you name a union, then you must create an instance of it before you can
reference its members. If you don't name a union, then the union becomes
its own instance.


 struct CHAR_INFO 
 {
     union
     {
         WCHAR UnicodeChar;
         CHAR  AsciiChar;
     }
 }

In this case, the anonymous union is also an instance of itself so you can
reference its members directly.

 CHAR_INFO chiFill;
 chiFill.UnicodeChar = '\x20';

However ...

 struct CHAR_INFO 
 {
     union Char
     {
         WCHAR UnicodeChar;
         CHAR  AsciiChar;
     }
     Char aChar;
 }

In this case, the union is named so you have to create an instance of it
first, then reference member through that instance.

 CHAR_INFO chiFill;
 chiFill.aChar.UnicodeChar = '\x20';


If you use the union name itself in the reference, then you need to only
reference 'static' members, and I'm not sure what that would be.
  
-- 
Derek
Melbourne, Australia
18/06/2005 12:46:21 PM
Thanks Derek...funny thing tho, this example is a part of some D code that compiled fine with D v0.97 in July 2004 ( somehow I missed retesting this code some 30 version of D ago :P ). Thanks again. David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Jun 17 2005
prev sibling parent reply Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
I believe it's intended to work this way... Make the union's name e.g. 
UChar and then declare it inside the struct, like:















David L. Davis wrote:































 
 Output:
 ----------
 C:\dmd>dmd needthis.d
 needthis.d(26): need 'this' to access member AsciiChar
-- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jun 17 2005
parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <d902fe$2gfd$1 digitaldaemon.com>, Tom S says...
I believe it's intended to work this way... Make the union's name e.g. 
UChar and then declare it inside the struct, like:















David L. Davis wrote:































 
 Output:
 ----------
 C:\dmd>dmd needthis.d
 needthis.d(26): need 'this' to access member AsciiChar
-- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Thanks Tomasz! BTW, your handle / alias "h3r3tic" reminds me of someone that had a similar name, who I remember made good levels / maps for "Heretic 2" back in the day. David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
Jun 17 2005
parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
David L. Davis wrote:
 BTW, your handle / alias "h3r3tic" reminds me of someone that had a similar
 name, who I remember made good levels / maps for "Heretic 2" back in the day.
Not me, sorry ;) I enjoyed playing Heretic2 but I never made a map for it -- Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Jun 18 2005