loop in class definition ?

Discussion in 'C++' started by vertigo, Aug 28, 2004.

  1. vertigo

    vertigo Guest

    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
    vertigo, Aug 28, 2004
    #1
    1. Advertising

  2. 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-lite/misc-technical-issues.html#faq-38.11
    Simon Saunders, Aug 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    587
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    416
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    449
    Jon Slaughter
    Oct 26, 2005
  4. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Decralation of class inside other class and definition outside this class

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 13, 2007, in forum: C++
    Replies:
    2
    Views:
    337
    Alf P. Steinbach
    Jul 13, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page