Archived

This topic is now archived and is closed to further replies.

class A uses B and B uses A

This topic is 5601 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have two classes A and B. A is defined using B B is defined using A I put the instruction ''class B;'' in A. So I thought that I could use the class B to build methods in A. But when I create in the class A a prototype like: int func(B* b); I can compile the class A. but if I define the method func: int func(B* b); int counter=b->GetCounter(); then I have an error message in A saying: H:\antSim2\Ant.cpp(51) : error C2027: use of undefined type ''Garden'' h:\antsim2\ant.h(43) : see declaration of ''Garden'' I can understand that I have to define the class B before using it but what are the advantage to use the instruction ''class A'' if I cannot use the class B in a method of the class A? Above all how can I resolve this problem? ps : I tried to put an #include <B.h> in A but I get lots of error messages. zip7000

Share this post


Link to post
Share on other sites
Try this, and be sure to use the ifndef/define in all your headers file !! That will avoid nested included headers problems !!


        
Your A.h header file :
#ifndef __A_H
#define __A_H

class B;

class A
{
void func(B *refClassB);
};

#endif

Your B.h header file :
#ifndef __B_H
#define __B_H

class B
{
void func(A *refClassA);
};

#endif


then in your main program you can include :

include "A.h"
include "B.h" // must be after A.h or else add class A; in B.h


__________________________



Bruno Wieckowski
Lead Programmer
Exood4 Studios



[edited by - brunow on August 15, 2002 8:33:48 AM]

Share this post


Link to post
Share on other sites
EDIT: See above

I think this is right:

in A.H:

#ifndef A_H
#define A_H

#include "B.h"

class B; // forward reference
class A
{
void func(B* b);
};

#endif

Try that pattern of declaration.

........

The #define wills stop you from getting circular errors
The forward reference allows you to use B in the declaration of func.

I'm not certain, you may be able to take out the include "B.h" with the forward reference there.

Repeat the same pattern with the B.h file.


[edited by - Waverider on August 15, 2002 8:31:11 AM]

Share this post


Link to post
Share on other sites
To add to what the other two have said.

To use a function of class B in a method of class A, define the method "func" for class A in your cpp file (obviously), and at the top of the cpp put

#include "B.h"

then everything should be ok.

NightWraith

Share this post


Link to post
Share on other sites
Logically, this will work.

Physically, circular dependencies are the root of complications in large projects. You should endeavor to remove this circular dependency. An excellent book on the subject is "Large Scale C++ Design" by Lakos.

Share this post


Link to post
Share on other sites
I strongly second what Stoffel has said.

Don''t neglect it otherwise you WILL regret it in the future.

Get in the habit of doing the right thing now before your project gets big.

Just because it works doesn''t mean its right or done.

Share this post


Link to post
Share on other sites