Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > loop in class definition ?

Reply
Thread Tools

loop in class definition ?

 
 
vertigo
Guest
Posts: n/a
 
      08-28-2004
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


 
Reply With Quote
 
 
 
 
Simon Saunders
Guest
Posts: n/a
 
      08-28-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Decralation of class inside other class and definition outside this class =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 2 07-12-2007 11:52 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM



Advertisments