| View previous topic :: View next topic |
| Author |
Message |
vertigo Guest
|
Posted: Sun Feb 27, 2005 1:52 pm Post subject: two classes - each has pointer to other |
|
|
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
|
|
| Back to top |
|
 |
Fraser Ross Guest
|
Posted: Sun Feb 27, 2005 2:45 pm Post subject: Re: two classes - each has pointer to other |
|
|
#Include "QueueHandler.h" in Queue.cpp or Queue.h.
Fraser.
"vertigo" <none (AT) none (DOT) pl> wrote
| Quote: | 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
|
|
|
| Back to top |
|
 |
davidrubin@warpmail.net Guest
|
Posted: Sun Feb 27, 2005 8:13 pm Post subject: Re: two classes - each has pointer to other |
|
|
vertigo wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Fraser Ross Guest
|
Posted: Mon Feb 28, 2005 1:33 pm Post subject: Re: two classes - each has pointer to other |
|
|
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.
| Quote: | 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
|
|
|
| Back to top |
|
 |
Clark S. Cox III Guest
|
Posted: Mon Feb 28, 2005 2:37 pm Post subject: Re: two classes - each has pointer to other |
|
|
On 2005-02-27 15:13:00 -0500, [email]davidrubin (AT) warpmail (DOT) net[/email] said:
| Quote: |
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
[email]clarkcox3 (AT) gmail (DOT) com[/email]
|
|
| Back to top |
|
 |
|