Jump to content
  • Advertisement
Sign in to follow this  
bobason456

my class structures?

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

Hello, I'm having problems with my class structures. Best just to look at the code below. Portals tell us what zones are connected to "this" zone. So my Portal Class has a pointer to ToZone the zone that this portal is referencing.
#ifndef _CPortal_H_
#define _CPortal_H_

#include "zone.h"

class CPortal
{
public:
	CZone			*ToZone;
};

#endif

Each Zone may have a number of portals. So in my Zone class I set up the list.
#ifndef _ZONE_H
#define _ZONE_H

#include "portal.h"

class CZone {
public:
	CPortal *TheList;
};

#endif

The Problem is that when I build it VS does not know what to do with this circular referencing and I get. 'portal.h' error C2143: syntax error : missing ';' before '*' 'portal.h' error C2501: 'CZone' : missing storage-class or type specifiers 'portal.h' error C2501: 'ToZone' : missing storage-class or type specifiers Whats the best way around this? Thanks DaveC

Share this post


Link to post
Share on other sites
Advertisement
#ifndef _CPortal_H_
#define _CPortal_H_

class CPortal;

#include "zone.h"

class CPortal
{
public:
CZone *ToZone;
};

#endif



#ifndef _ZONE_H
#define _ZONE_H

class CZone;

#include "portal.h"

class CZone {
public:
CPortal *TheList;
};

#endif


Something like this might take care of your problem.
-edit- Since it looks like your zone.h is the one getting included first (which in turn includes portal.h) the second bolded addition should fix your problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by bobason456
Actually it worked when I put empty class defs in both includes.

in portal.h
class CPortal;

and in zone.h
class CZone;


It didn't work with just one? I usually just add them when I need them (when it won't compile).

Share this post


Link to post
Share on other sites
Quote:
It didn't work with just one? I usually just add them when I need them (when it won't compile).

It's good practice to build your header files so they will compile even if you create a blank source file and include only the header file in question. They should include/declare everything they need to function by themselves. There are quite a few reasons for doing this, such as:

1. Minor changes to your include order or header file system later on won't cause a maze of include order problems (very important when you start having hundreds of header files in one project)
2. It generally means your header files are better encapsulated, and as such are less dependent on others and are more robust
3. It presents a clear indication of the dependencies a module has without hunting through the code
4. In the source file for that module, you can just include the header file alone and not worry about any other headers

Share this post


Link to post
Share on other sites
Thanks.. but I though it was good practice to place Class definitions in Header files and Class member function code in source files. If I follow that rule then my problem is that I'm defining member variables of type "the other" class for each others class, um?

So I'll get the same problem with no code in my source files.

Share this post


Link to post
Share on other sites
I think you misunderstood my post. It wasn't related to the original question, it was just a comment on header file systems in general. The way to solve the problem you had is to use forward declarations for the classes in question, as already covered, and nothing I said precluded doing that. Each of these header files only need to know that the other class exists, but not the structure of it, so it doesn't need to (in fact, I'd say it shouldn't) include it. The forward declaration is sufficient for it to function in its entirety, from the code presented. If in part of the code for these functions, they have to operate on one another (access a member, create a new object, etc), then they should include the other file, however a more robust header file structure is needed to prevent cyclic dependency issues in cases like this, if you use inlined functions or templates.

Whatever the case however, my point was I believe I should be able to include any header file by itself in a blank source file, and have it compile without errors.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!