Reputation: 787
I have three self defined data types.
- struct A
- struct B
- union U
And here is how they are related to each other.
union U; // This is a forward declaration
struct A{
U u;
};
struct B{
A a;
};
union U{
B b;
int integer;
};
And I end up getting the same type of error
error: field 'type_union' has incomplete type 'Union'
I've tried various types of forward type declarations. But, one of these types end up being a incomplete type.
There is a cyclic dependency between these types that seems impossible to work. How can I make them work. OR any other workaround.
Upvotes: 0
Views: 46
Reputation: 9678
In this case, you cannot make this work. U literally contains an instance of itself!!
Other than that, the rules are:
union U;
struct A{
U u; ///< this can't work. sizeof U needs to be known!
};
union U;
struct A{
U* u; ///< this can work, pointers are of known size!
};
Forward declaration only really work in this context, if you are defining a pointer to the unknown object. Your case breaks down horribly here:
union U{
B b; //< B contains A, which contains U, which you are defining!
int integer;
};
In your case you'd have to break one of the dependencies via a pointer & forward declaration. However, something seems a little fishy with your design here? Possibly rethink your design instead?
struct A{
U u; //< either this has to be a pointer
};
struct B{
A a; //< or this
};
union U{
B b; //< or this
int integer;
};
Upvotes: 2