www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - this() to access unions?

reply Tyro <ridimz_at yahoo.dot.com> writes:
The code below spews out the error "need 'this' to access member" ... 
each time a member of theValue is accessed. I'm thoroughly confused. 
Please explain.

Andrew

=====endian.d=====
import std.stdio;

union theValue {
   uint bits32;
   char[4] bytes;
}

int main()
{
   theValue.bytes[0]=0;
   theValue.bytes[1]=1;
   theValue.bytes[2]=0;
   theValue.bytes[3]=0;

   if(theValue.bits32==256)
     writefln("LittleEndian");

   return 0;
}
Nov 22 2004
next sibling parent "Simon Buchan" <currently no.where> writes:
On Mon, 22 Nov 2004 05:47:09 -0500, Tyro <ridimz_at yahoo.dot.com> wrote:

 The code below spews out the error "need 'this' to access member" ...  
 each time a member of theValue is accessed. I'm thoroughly confused.  
 Please explain.

 Andrew

 =====endian.d=====
 import std.stdio;

 union theValue {
    uint bits32;
    char[4] bytes;
 }

 int main()
 {
    theValue.bytes[0]=0;
    theValue.bytes[1]=1;
    theValue.bytes[2]=0;
    theValue.bytes[3]=0;

    if(theValue.bits32==256)
      writefln("LittleEndian");

    return 0;
 }
You only made a union type. You need to declare an instance like theValue aValue; pretty much anywhere. Unfortunately D doesnt allow the old <class type> [tag] {<class body>} [value list]; Which saved some keystrokes (Why cant I make an instance of a custom, one-off struct, Walter?) -- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 22 2004
prev sibling parent reply Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> writes:
You defined the type "theValue" but there is no var of this type...








Thomas

Tyro schrieb am Mon, 22 Nov 2004 05:47:09 -0500:
 The code below spews out the error "need 'this' to access member" ... 
 each time a member of theValue is accessed. I'm thoroughly confused. 
 Please explain.

 Andrew

=====endian.d=====
 import std.stdio;

 union theValue {
    uint bits32;
    char[4] bytes;
 }

 int main()
 {
    theValue.bytes[0]=0;
    theValue.bytes[1]=1;
    theValue.bytes[2]=0;
    theValue.bytes[3]=0;

    if(theValue.bits32==256)
      writefln("LittleEndian");

    return 0;
 }
Nov 22 2004
parent Tyro <ridimz_at yahoo.dot.com> writes:
Duh! Can't believe I missed that. Thanks guys.

Andrew

Thomas Kuehne wrote:
 You defined the type "theValue" but there is no var of this type...
 






 
 Thomas
 
 Tyro schrieb am Mon, 22 Nov 2004 05:47:09 -0500:
 
The code below spews out the error "need 'this' to access member" ... 
each time a member of theValue is accessed. I'm thoroughly confused. 
Please explain.

Andrew

=====endian.d=====
import std.stdio;

union theValue {
   uint bits32;
   char[4] bytes;
}

int main()
{
   theValue.bytes[0]=0;
   theValue.bytes[1]=1;
   theValue.bytes[2]=0;
   theValue.bytes[3]=0;

   if(theValue.bits32==256)
     writefln("LittleEndian");

   return 0;
}
Nov 22 2004