Home > Blockchain >  Creating variable without defining the variables type class yet
Creating variable without defining the variables type class yet

Time:11-15

class Component {
    public:
        Entity *parent = nullptr;
};

class Entity {
    public:
        Component components[25];
};

I am trying to create an entity component system, and above I have an issue. In the component class I am creating a pointer variable with the datatype being the "Entity" class, even though that gets defined later. Is there a way I can do this without an error occurring?

I tried using auto *parent = nullptr; but that doesn't work.

CodePudding user response:

You need to declare Entity as a class, so the compiler knows what type this is.

    class Entity;

Usually you would put these sorts of forward declarations in a generic header file, then define each class completely in its own Classname.cpp file.

  • Related