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

Started by
13 comments, last by mickey 21 years, 5 months ago
ie, // a.h #include "b.h" class A { B b; // error, missing storage type identifier..., ... }; // b.h #include "a.h" class B { ... }; thanks,
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Advertisement
Where does header file b use the header file a?
way to do this sorta thing

_______header A_____

class B;

class A
{
B *thing;
};


_______header B_____
#include "header A"

class B
{
A thing;
};
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]
class B;

class A{
public:
B* myB; // use a pointer because at the moment the B-class is nothing
};

class B{
public:
A myA; // No need for a pointer because A-class is filled already
};
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]
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
There''s a tutorial that mentions this very thing here on GD.net.

http://www.gamedev.net/reference/programming/features/orgfiles/

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
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>  
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,
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
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.

This topic is closed to new replies.

Advertisement