C++ circular include

后端 未结 4 1206
野的像风
野的像风 2020-12-06 03:26

I can\'t solve this circular dependency problem; always getting this error: \"invalid use of incomplete type struct GemsGame\" I don\'t know why the compiler doesn\'t know t

4条回答
  •  难免孤独
    2020-12-06 03:39

    If you deference the pointer and the function is inline you will need the full type. If you create a cpp file for the implementation you can avoid the circular dependecy (since neither of the class will need to include each others .h in their headers)

    Something like this:

    your header:

    #ifndef GEMELEMENT_H_INCLUDED
    #define GEMELEMENT_H_INCLUDED
    
    class GemsGame;
    
    class GemElement {
        private:
            GemsGame* _gemsGame;
    
        public:
            GemElement();
    };
    
    
    #endif // GEMELEMENT_H_INCLUDED
    

    your cpp:

    #include "GenGame.h"
    GenElement::GenElement()
    {
       _gemsGame = application.getCurrentGame();
       _gemsGame->getGemsVector();
    }
    

提交回复
热议问题