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

"Invalid base class" error when trying to inherit from derived struct (C++)

I’ve got a "Base" struct, an "NPC" struct derived from the "Base". All works perfectly fine. But when I try to create a new struct called "PC" from the "NPC" struct, I get an error: "invalid base class".
What’s the problem? Is it not possible to create a struct from a derived struct?

struct Base
{
    char* name = 0;

    int MaxHP = 0;
    int CurrHP = 0;
};


struct NPC : Base
{
    int gold = 0;
    int stats[];
};

struct PC : NPC // I get the error here
{
    unsigned int ID = 0;
};

>Solution :

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

When you wrote:

struct NPC : Base
{
    int gold = 0;
    int stats[]; //NOT VALID, this is a definition and size must be known
};

This is not valid as from cppreference:

Any of the following contexts requires type T to be complete:

  • declaration of a non-static class data member of type T;

But the type of the non-static data member stats is incomplete and hence the error.

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