Enable file clutter

I have 2 classes - one contains information about the object, the other contains information about the components. Now the problem is that the Entity class needs the Component class, which is already defined for its use in the vector of children, but at the same time, the component requires Entity to declare it as a parent (I keep everything related between them). This causes strange errors, although IntelliSense says that all of this is already defined.

How can I overcome this difficulty?

+3
source share
4 answers

component.h:

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

entity.h:

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

The only drawback here is that the built-in methods in the .h component cannot use Entity methods.

+6

, :

Entity.h:

#include <vector>

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

Component.h:

#include <Entity.h>

class Component : public Entity { ... };

- Component vector Component s:

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.
+3

+2

, Component vector (.. vector<Component*> ( ptr) vector<Component>), Component Entity Component.

+1

Source: https://habr.com/ru/post/1758917/


All Articles