简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM