For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > August 2004 > loop in class definition ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author loop in class definition ?
vertigo

2004-08-28, 8:56 am

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


Pascal Bourguignon

2004-08-28, 3:57 pm

vertigo <none@microsoft.com> writes:

> 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 class declarations:

class classA;
class classB{
classA* ptr;
};

class classA{
classB* ptr;
};

--
__Pascal Bourguignon__ http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com