Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating variable without defining the variables type class yet

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading