# my class structures?

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

## 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 on other sites
#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 on other sites
Yo Da Man, thanks.

##### Share on other sites
Actually it worked when I put empty class defs in both includes.

in portal.h
class CPortal;

and in zone.h
class CZone;

##### Share on other sites
Quote:
 Original post by bobason456Actually it worked when I put empty class defs in both includes.in portal.hclass CPortal;and in zone.hclass CZone;

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

##### 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 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 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 on other sites
ok, I understand now.. Thanks.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 23
• 36
• 16
• 75