Your current design contains a serious flaw which is known as circular dependency. It will never compile, and the best way to fix it is to redesign your class hierarchy.
To give you the clue why it can never compile, lets simplify the situation by removing templates:
class B;
class A {
B b;
};
class B {
A a;
};
Now try to think as a compiler. To know how much space is needed for allocation of class A
(and compiler has to know that at compile-time), it needs to know how much space is needed for allocation of class B
, and vice versa. Clearly, this is a circular dependency. You can try to compile it yourself and see that compiler complains about it.
One possible way to fix that would be switching to pointers (or references):
class B;
class A {
B* b; // B& b; is possible too
};
class B {
A a;
};
This should compile just fine. The reason is that now the compiler knows that b
is a pointer, and the space needed to accommodate that pointer is fixed (4 bytes for 32-bit targets, and 8 bytes for 64-bit targets accordingly).
NOTE: Pointer/reference to a
in B
is unnecessary because A
is already defined at that point, and therefore we can use it as a direct member in B
.
In your situation you complicated things even further with templates, and while you could utilize the proposed fix (using pointers/references) and combine it with templates, I don't recommend it. Your code has to be totally redesigned.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…