www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Forward reference in struct

reply "Simen Haugen" <simen norstat.no> writes:
I've hit a problem with forward referencing. Is it possible to manually add 
a forward reference, or isn't what I'm trying to do here allowed?

struct A {
 B b;
}

struct B {
 union {
  int i;
  A a;
 }
}


t.d(5): struct test.A unable to resolve forward reference in definition
t.d(10): anonymous union unable to resolve forward reference in definition
t.d(9): struct test.B unable to resolve forward reference in definition 
Oct 08 2007
next sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Simen Haugen wrote:
 I've hit a problem with forward referencing. Is it possible to manually add 
 a forward reference, or isn't what I'm trying to do here allowed?
 
 struct A {
  B b;
 }
 
 struct B {
  union {
   int i;
   A a;
  }
 }
 
 
 t.d(5): struct test.A unable to resolve forward reference in definition
 t.d(10): anonymous union unable to resolve forward reference in definition
 t.d(9): struct test.B unable to resolve forward reference in definition 

Let's disregard the int. You essentially have this: ----- struct A { B b; } struct B { A a; } ----- Now ask yourself this question: How big is A? Obviously, since it's a struct with 1 member, it's equal to the size of B. But how big is B? By the same reasoning, it's as big as A. You have an infinite "member recursion", which isn't allowed. To fix this, you may want to change A.b and/or B.a to a pointer, or change A and/or B from a struct to a class (since classes are always stored by reference, which is essentially an implicit pointer).
Oct 08 2007
parent "Simen Haugen" <simen norstat.no> writes:
"Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message 
news:fed9j3$udv$1 digitalmars.com...
 Let's disregard the int. You essentially have this:
 -----
 struct A {
     B b;
 }
 struct B {
     A a;
 }
 -----
 Now ask yourself this question: How big is A?
 Obviously, since it's a struct with 1 member, it's equal to the size of B. 
 But how big is B? By the same reasoning, it's as big as A.
 You have an infinite "member recursion", which isn't allowed.

 To fix this, you may want to change A.b and/or B.a to a pointer, or change 
 A and/or B from a struct to a class (since classes are always stored by 
 reference, which is essentially an implicit pointer).

Of course... I changed one to use pointers instead and it fixed the problem. Thanks to both Frits and Regan.
Oct 08 2007
prev sibling parent Regan Heath <regan netmail.co.nz> writes:
Simen Haugen wrote:
 I've hit a problem with forward referencing. Is it possible to manually add 
 a forward reference, or isn't what I'm trying to do here allowed?
 
 struct A {
  B b;
 }
 
 struct B {
  union {
   int i;
   A a;
  }
 }
 
 
 t.d(5): struct test.A unable to resolve forward reference in definition
 t.d(10): anonymous union unable to resolve forward reference in definition
 t.d(9): struct test.B unable to resolve forward reference in definition 

This is not allowed because it's impossible to resolve, I believe. At the point where you declare A the compiler wants to calculate the size of the structure, it can/will delay this till it finds a definition for B, but when it finds B it also needs to calculate the size of B, but to calculate the size of B it needs to know the size of A, but to calculate the size of A it needs to know the size of B, but to calculate the size of B it needs to know the size of A, but to calculate the size of A it needs to know the size of B, ... Try:
 struct A {
  B* b;
 }

instead. I don't have DMD here so I can't test it but this should be possible as pointers have a fixed/known size. Regan
Oct 08 2007