C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

two classes - each has pointer to other

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
vertigo
Guest





PostPosted: Sun Feb 27, 2005 1:52 pm    Post subject: two classes - each has pointer to other Reply with 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
Fraser Ross
Guest





PostPosted: Sun Feb 27, 2005 2:45 pm    Post subject: Re: two classes - each has pointer to other Reply with quote



#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





PostPosted: Sun Feb 27, 2005 8:13 pm    Post subject: Re: two classes - each has pointer to other Reply with quote




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





PostPosted: Mon Feb 28, 2005 1:33 pm    Post subject: Re: two classes - each has pointer to other Reply with quote

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





PostPosted: Mon Feb 28, 2005 2:37 pm    Post subject: Re: two classes - each has pointer to other Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.