Forward reference in struct
Regan Heath
regan at netmail.co.nz
Mon Oct 8 06:01:55 PDT 2007
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
More information about the Digitalmars-d-learn
mailing list