By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,798 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,798 IT Pros & Developers. It's quick & easy.

loop in class definition ?

P: n/a
Hello
I have:

classA.h file:
#include "classB.h"
classA{
classB *ptr;
};

classA.c file:
#include "classA.h"
.......

classB.h file:
#include "classA.h"
classB{
classA *ptr;
}

classB.c file:
#include "classB.h"
.......

I compile classA.c and classB.c and later link all together.
My problem is that in this case there is redefinition of classA and
classB (because classA.h and classB.h files are included two times).
When i used in *.h files:
#ifndef XXX
#define XXX
......
#endif

*.h files are included once but i do not see their definitions and can
not compile (classB.h do not see definition for classA and classA.h do
not see definition for classB).

How can i solve this problem ?

Thanx
Michal
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sat, 28 Aug 2004 14:23:47 +0200, vertigo wrote:
Hello
I have:

classA.h file:
#include "classB.h"
classA{
classB *ptr;
};

classA.c file:
#include "classA.h"
......

classB.h file:
#include "classA.h"
classB{
classA *ptr;
}

classB.c file:
#include "classB.h"
......

I compile classA.c and classB.c and later link all together.
My problem is that in this case there is redefinition of classA and
classB (because classA.h and classB.h files are included two times).
When i used in *.h files:
#ifndef XXX
#define XXX
.....
#endif

*.h files are included once but i do not see their definitions and can
not compile (classB.h do not see definition for classA and classA.h do
not see definition for classB).

How can i solve this problem ?


Use forward declarations:

classA.h:
class B;
class A
{
class B* ptr;
};

classA.c:
#include "classA.h"
#include "classB.h"
....

(and do the same for classB obviously).

This is covered by the C++ FAQ Lite:

http://www.parashift.com/c++-faq-lit...html#faq-38.11

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.