views:

79

answers:

4

I'm having 2 classes - one holding Entity information other holding Component information. Now the problem is that the Entity class needs Component class already defined for using it in vector of children, but at the same time Component needs Entity to declare it as it's parent (I'm keeping everything linked in between). This puts out strange errors, even though IntelliSense says it's all already defined.

How can I overcome this difficulty?

+5  A: 

component.h:

class Entity;
class Component {
    ...
    Entity *parent;
};

entity.h:

#include "component.h"
class Entity {
    ...
}

The only drawback here is that inline methods in component.h cannot use Entity methods.

Graeme Perrow
This worked thanks!Still just in case I'd like to see is there a way to point _and_ use methods. Any advices?
Johnny
You can use methods in the .cpp file, just not the header.
Justin Ardini
You can certainly have methods in the Component class that call Entity methods, you just can't have them inline within the .h file. You'd have to add a prototype for them in the class and then have the implementation in the component.cpp file (which includes both component.h and entity.h).
Graeme Perrow
Oh, then that's ok! Cause I just need to link them and use them in the source file.Thanks!
Johnny
+2  A: 

Use forward declaration

Arseny
+2  A: 

It sounds like you have this:

Entity.h:

#include <vector>

class Entity {
public:
    std::vector<Component> children;
};

Component.h:

#include <Entity.h>

class Component : public Entity { ... };

One way around the issue is to forward-declare the Component class and use a vector of pointers to Components:

Entity.h:

#ifndef ENTITY_H
#define ENTITY_H
#include <vector>

class Component;    // Forward declaration.

class Entity {
public:
    std::vector<Component*> children;
};

#endif /* ndef ENTITY_H */

Component.h:

#ifndef COMPONENT_H
#define COMPONENT_H
#include <Entity.h>    // To allow inheritance.

class Component : public Entity { ... };

#endif /* ndef COMPONENT_H */

Entity.cpp:

#include <Entity.h>
#include <Component.h>    // To access Component members.
Jon Purdy
Aaah, you advise to put it into cpp file, and make a forward declaration in the header? Thanks!This is most likely what I need!Still one thing bothers me, you've included Entity.h, then you include Component.h, wouldn't that include Entity twice? Maybe I could use a define to ignore including Entity in compoenent.h?
Johnny
I omitted the include guards for brevity because they're quite common practice. I'll put them in for completeness.
Jon Purdy
+1  A: 

One option is if you just use pointers to Component in your vector (ie vector<Component*> (or a smart ptr variant) instead of vector<Component>), you could forward declare the Component class and your Entity class declaration wouldn't need the Component definition.

bshields
I do use vector<Component*>, but that isn't solving it since I need a this ptr from entity to be assigned to pParent in Copmonent object.
Johnny
Are you writing all the code in the header files? You generally only want to be making declarations in the header file. If you forward declare (`class Component;`) before your `Entity` definition, you won't have to include `Component.h` in your header file. You will need it in the source file when you actually initialize/use your `Component` object, but including `Component.h` from `Entity.cpp` does not introduce a circular reference.
bshields
Of course not, header is only for definitions.However I've figured it out - I'm including them in source files, and making use of #ifndef include protection (you know what I mean).
Johnny