开发者

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?


Forward declare B (or A )

class B; //forward declaration of B

class A
{
   B *b;
};

class B
{
   A *a;
};


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;

};


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜