mutual template definition

Hello, this code:

template <class tB>
struct A{
typedef float atype;
typedef typename tB::btype typeB;

};

template <class tA>
struct B{
typedef float btype;
typedef typename tA::atype typeA;
};

struct MyB;
struct MyA: public A<MyB>{};
struct MyB: public B<MyA>{};

int main(int argc, char *argv[])
{
}

does not compile because
"main.cpp:6: error: invalid use of incomplete type ‘struct MyB’".

Basically the compiler cannot solve the loop because definition
of A depends on definition of B an viceversa.
Is there a way to sort this out?
thanks,
Rob
Basically the compiler cannot solve the loop because definition
of A depends on definition of B an viceversa.
this is very far from what the compiler is saying. It is telling you that you cannot use undefined classes as template arguments.
In general, a forward declaration is only useful if you want to reference a pointer (or reference) to the type. The compiler usually knows how large a pointer (or reference) is, but it cannot know how big a struct/class is going to be; hence the error.
That's actually an interesting problem. I can't think of a good way around it.

The only thing I can think of would be to take another template argument... one that specifies atype/btype.
Topic archived. No new replies allowed.