Jump to content
  • Advertisement

Archived

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

mickey

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

This topic is 5728 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
Advertisement
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
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
};

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!