CromTheDestroyer
CromTheDestroyer

Reputation: 3766

C++ recursive data types

A bit of a noob question: I need classes A and B such that A has a B* member and B has an A* member.

When compiling I get "error: ISO C++ forbids declaration of ‘B’ with no type". How can I get around this?

Upvotes: 3

Views: 683

Answers (3)

Necrolis
Necrolis

Reputation: 26181

You can also inline forward declare one of the classes if nothing else is using it: class B* pMemberB; al la C style

Upvotes: 2

Prasoon Saurav
Prasoon Saurav

Reputation: 92942

Forward declare B (or A )

class B; //forward declaration of B

class A
{
   B *b;
};

class B
{
   A *a;
};

Upvotes: 10

shuttle87
shuttle87

Reputation: 15964

Forward declare one of class a or b.

class b; //forward declaration

class a{
//class a stuff
b* ptrtoB;

};


class b{
//class b stuff
a* ptrtoA;

};

Upvotes: 6

Related Questions