two classes - each has pointer to other

Discussion in 'C++' started by vertigo, Feb 27, 2005.

  1. vertigo

    vertigo Guest

    Hello
    I have QueueHandler.h:

    #include "Queue.h"
    class Queue;
    class QueueHandler{
    ....
    Queue *ptrQ;
    }

    And in file Queue.h:
    class QueueHandler;
    class Queue{
    QueueHandler *ptrH;
    }

    the problem durring compilation is in Queue.cpp when i want to call
    method on QueueHandler (ptrH) object:
    Queue.cpp error: invalid use of undefined type of 'struct QueueHandler'
    Queue.h error: forward declaration of 'struct QueueHandler'

    What can i do to solve this problem ?

    Thanx
    Michal
    vertigo, Feb 27, 2005
    #1
    1. Advertising

  2. vertigo

    Fraser Ross Guest

    #Include "QueueHandler.h" in Queue.cpp or Queue.h.

    Fraser.


    "vertigo" <> wrote in message
    news:cvsjhs$2ia$...
    > Hello
    > I have QueueHandler.h:
    >
    > #include "Queue.h"
    > class Queue;
    > class QueueHandler{
    > ...
    > Queue *ptrQ;
    > }
    >
    > And in file Queue.h:
    > class QueueHandler;
    > class Queue{
    > QueueHandler *ptrH;
    > }
    >
    > the problem durring compilation is in Queue.cpp when i want to call
    > method on QueueHandler (ptrH) object:
    > Queue.cpp error: invalid use of undefined type of 'struct QueueHandler'
    > Queue.h error: forward declaration of 'struct QueueHandler'
    >
    > What can i do to solve this problem ?
    >
    > Thanx
    > Michal
    >
    >
    Fraser Ross, Feb 27, 2005
    #2
    1. Advertising

  3. vertigo

    Guest

    vertigo wrote:
    > Hello
    > I have QueueHandler.h:
    >
    > #include "Queue.h"
    > class Queue;
    > class QueueHandler{
    > ...
    > Queue *ptrQ;
    > }
    >
    > And in file Queue.h:
    > class QueueHandler;
    > class Queue{
    > QueueHandler *ptrH;
    > }


    These must be declared and implemented in the same component (.h/.cpp
    pair). Otherwise you introduce a cyclic dependency making the two
    classes more difficult to test. This is indicative of a suboptimal
    design. In any case, it is not clear why Queue uses QueueHandler in the
    interface. /david
    , Feb 27, 2005
    #3
  4. vertigo

    Fraser Ross Guest

    You are saying that 2 classes with an association with one another must be
    defined in the same unit. That is not the case at all. Using pointers in
    general removes dependecies that are present with aggregation. There is no
    dependency between these classes apart from the need for forward
    declarations or class definitions.

    Fraser.


    > These must be declared and implemented in the same component (.h/.cpp
    > pair). Otherwise you introduce a cyclic dependency making the two
    > classes more difficult to test. This is indicative of a suboptimal
    > design. In any case, it is not clear why Queue uses QueueHandler in the
    > interface. /david
    >
    Fraser Ross, Feb 28, 2005
    #4
  5. On 2005-02-27 15:13:00 -0500, said:

    >
    > vertigo wrote:
    >> Hello
    >> I have QueueHandler.h:
    >>
    >> #include "Queue.h"
    >> class Queue;
    >> class QueueHandler{
    >> ...
    >> Queue *ptrQ;
    >> }
    >>
    >> And in file Queue.h:
    >> class QueueHandler;
    >> class Queue{
    >> QueueHandler *ptrH;
    >> }

    >
    > These must be declared and implemented in the same component (.h/.cpp
    > pair).


    That is not true:

    //QueueHandler.h:
    class Queue;
    class QueueHandler
    {
    Queue *ptrQ;
    }

    //Queue.h:
    class QueueHandler;
    class Queue
    {
    QueueHandler *ptrH;
    }


    //QueueHandler.cpp:
    #include "Queue.h"
    #include "QueueHandler.h"

    /*QueueHandler's implementation*/

    //Queue.cpp:
    #include "Queue.h"
    #include "QueueHandler.h"

    /*Queue's implementation*/


    --
    Clark S. Cox, III
    Clark S. Cox III, Feb 28, 2005
    #5
    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. Gizmo
    Replies:
    2
    Views:
    275
    Christian Jaeger
    Oct 29, 2003
  2. Kent
    Replies:
    2
    Views:
    3,382
    Derek
    Dec 23, 2003
  3. Manuel Bleichner

    Classes referencing each other

    Manuel Bleichner, Sep 1, 2006, in forum: Python
    Replies:
    8
    Views:
    393
    Manuel Bleichner
    Sep 4, 2006
  4. al pacino
    Replies:
    2
    Views:
    272
    al pacino
    Mar 11, 2006
  5. stefven blonqhern

    two classes #including each other

    stefven blonqhern, Aug 24, 2007, in forum: C++
    Replies:
    7
    Views:
    359
    Pete Becker
    Aug 25, 2007
Loading...

Share This Page