Jake
Jake

Reputation: 13

Forward declaration of class / Invalid use of incomplete type

I have six classes, obviously this isn't my actual code, just a simple version

BaseA:

class BaseA {
public:
    BaseA();
    virtual ~BaseA();
    void update() { // the body functions would normally be in a seperate file
        for (auto iter : list_of_bs) {
            iter->update();
        }
    }
private:
    vector<BaseB*>* list_of_bs;
};

BaseB:

class BaseB {
public:
    BaseB();
    virtual ~BaseB();
    void update() { // the body functions would normally be in a seperate file
        for (auto iter : list_of_cs) {
            iter->update();
        }
    }
private:
    vector<BaseC*>* list_of_cs;
};

BaseC

class BaseC {
public:
    BaseC();
    virtual ~BaseC();
    void update() { // the body functions would normally be in a seperate file
        // do whatever
    }
};

Then I have three other classes, A, B, and C that inherit each of their respective base classes. I add an instance of B or C the list_of_bs/list_of_cs so it can have its own code to be run when the update function is called.

The problem is, it comes up with errors "Forward declaration of class / Invalid use of incomplete type" in various files. How can I set up my system so that it doesn't have these errors?

If you want to see my actual code, you can find it here: https://github.com/Ja-ake/openworld

Read the README.

Upvotes: 1

Views: 6635

Answers (1)

merlin2011
merlin2011

Reputation: 75565

The error you are getting is the result of using a forward declaration and then trying to call a method or access a member without providing the full declaration.

The fix is to make sure you always #include the .h file containing the full class definition (not necessarily the cc file that contains the method definitions) before calling any method on the class.

Upvotes: 6

Related Questions