two classes - each has pointer to other

V

vertigo

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
 
D

davidrubin

vertigo said:
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
 
F

Fraser Ross

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.
 
C

Clark S. Cox III

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*/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top