Archived

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

header a needs header b, haeder b needs header a too, how?

This topic is 5512 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

ie, // a.h #include "b.h" class A { B b; // error, missing storage type identifier..., ... }; // b.h #include "a.h" class B { ... }; thanks,

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
way to do this sorta thing

_______header A_____

class B;

class A
{
B *thing;
};


_______header B_____
#include "header A"

class B
{
A thing;
};

Share this post


Link to post
Share on other sites
If I remember correctly (been a while since I've done C++), you can put 'class B;' before your declaration of class A. That should solve your problem.

Edit: looks like we both posted at the same time.

[edited by - A. Buza on November 9, 2002 2:59:45 AM]

Share this post


Link to post
Share on other sites
hey, i've tried already all those and it won't work, pointers won't also work if you started using the member functions,

edit: if you guys have time, try it, make a main.cpp, then create two header files and include them to each oter,

[edited by - mickey on November 9, 2002 8:42:07 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by mickey
hey, i've tried already all those and it won't work, pointers won't also work if you started using the member functions,

edit: if you guys have time, try it, make a main.cpp, then create two header files and include them to each oter,
Sure it works, and I won't even try it. Not to include both header files in eachother, but to get around that. If you have interdependent classes, always use pointers as that will be less confusing. And remember that, if you use pointers, you won't instanciate the member objects (they're only pointers), and you need to create them (new) in the constructor and delete them in the destructor.

A note here though: WTF are you trying to do? If you have two objects that both contain the other object, you'll have a never ending recursion problem. If A shall contain a B, and B contain an A. What will happen when you create A? A contains a B, so B is created, which contains an A, which contains a B, and so on... It is not possible to do this without using pointers, though.

Perhaps what you are looking for: (makes no sense, but whatever)

File: Server.h
  #ifndef _SERVER_H_
#define _SERVER_H_

class Client;

class Server
{
public:
Server() { this->pClient = new Client(this); }
~Server() { if(this->pClient) delete this->pClient; }

private:
Client *pClient;
};

#endif
File: Client.h
  #ifndef _CLIENT_H_
#define _CLIENT_H_

#include "Server.h"

class Client
{
public:
Client(Server *pParent) { this->pServer = pParent; }

private:
Server *pServer;
};

#endif
File: main.cpp
  #include "Server.h"

#include "Client.h"

main()
{
Server OurServer;
}{/source]

<SPAN CLASS=editedby>[edited by - CWizard on November 9, 2002 10:50:43 AM]</SPAN>

Share this post


Link to post
Share on other sites
hi CWizard,

yes, i am actually using pointers,

my Ball class needs a pointer to my Paddle class object, Power up, Particle System, my camera etc..,

my powerup class too needs my Ball class object too, so i declared a pointer inside my powerup class to a ball class object,

so, my ball class includes Power up header, and my power up class includes Ball class'' header too, and they won''t compile anymore,

btw.., your server.h, it doesn''t include client.h? using a member function of client would work??

many thanks,

Share this post


Link to post
Share on other sites
The thing is, you can''t include B.h in A.h, if B.h includes A.h. You need to get around that, so your header files aren''t dependent on eachother. This is usually not a problem, beacuse (in most cases) you shouldn''t access stuff declared in one header file from another header file. When it comes to members etc, these shouldn''t be accessed from a header file, but from the implementation file (.cpp) which can include both headers.

When you find yourself in Headers'' Hell (TM), it usually indicates that you have a too complex or badly designed organization. You''ll need to step back and rethink your file structure, and what is declared in which.

Share this post


Link to post
Share on other sites
ah i see, implementation file, okey i'll try that now thanks!

btw, if you have time, why don't you help me on the 'design' part?

prefix with C means class

you see, i have CGameObject acting as base,
then CBall : public CGameObject
CPaddle : public CGameObject
CLevel : public CGameObject
CCamera
CParticleSystem

okey, I made all my game objects inherit from one base CGameObject so i could take advantage of dynamic binding, however, my CBall needs all the classes, cam, particle, paddle, so i use pointers to store them.., how about you? any suggestions?

thanks,

Edit: okey it works now thanks a lot! i forgot to mention, my CPaddle also needs CBall, so how do you design this then?

[edited by - mickey on November 11, 2002 6:22:31 AM]

Share this post


Link to post
Share on other sites
I would say: use pointers everywhere, and don''t access any members of other objects in any header file.

I don''t know the complete picture of your code, but from what I can read, you should be able to use this matrix to read out what to include in what file:
====================+===============================================================================
File vv | Files to include -->
====================+===============================================================================
CGameObject.h | - - - - - -
CGameObject.cpp | CGameObject.h - - - - -
--------------------+-------------------------------------------------------------------------------
CBall.h | CGameObject.h - CPaddle.h CLevel.h CCamera.h ?
CBall.cpp | - CBall.h - - - -
--------------------+-------------------------------------------------------------------------------
CPaddle.h | CGameObject.h - - - - -
CPaddle.cpp | - CBall.h CPaddle.h - - -
--------------------+-------------------------------------------------------------------------------
CLevel.h | CGameObject.h - - - - -
CLevel.cpp | - - - CLevel.h - -
--------------------+-------------------------------------------------------------------------------
CCamera.h | ? - - - - -
CCamera.cpp | - - - - CCamera.h -
--------------------+-------------------------------------------------------------------------------
CParticleSystem.h | ? - - - - -
CParticleSystem.cpp | - - - - - CParticleSystem.h
====================+===============================================================================
Then you will need to sacrife your CBall.h file, and in it declare all other classes it needs. Just put this above your class definition in CBall.h:
#include "CBall.h"

class CPaddle;
class CLevel;
class CCamera;
class CParticleSystem;

class CBall : CGameObject
{
// ...
}
Hope that helps in some way. You will probably need to correct and modify it to work in your project, but there you go...

Share this post


Link to post
Share on other sites
some class''s header

  
#ifndef SOME_CLASS_H

#define SOME_CLASS_H // this keeps other headers from including us again



class some_class; // tell everything there is a class named some_class


#include "whatever we need" // include whatever we need here, it''s okay if they try to include us too.


class some_class
{
// stuff goes here

};

#endif


This method rocks because the compiler will see something like this once the preprocessor has finished mangling your code:

  
class a;
class b;
class c;
class d;

class d { . . . };
class c { . . . };
class b { . . . };
class a { . . . };


And since all the classes are declared first, there shouldn''t be any problems.

Share this post


Link to post
Share on other sites
CWizard: hey thanks for your insights, base on your table, i came up with this, there''ll be only one header file include in each of my class, that is, CGameObject.h, all other will be in the .cpp, so in my CBall, the CPaddle.h, Clevel.h, CCamera.h will all be in my Cball''s .cpp, then the class declaration would be on top,

hiya smart_idiot, that won''t work when you tried including each header to each other, ie, header A includes header B and header B includes header A and you started using their functions,

Share this post


Link to post
Share on other sites