C++ class forward declaration

后端 未结 8 618
春和景丽
春和景丽 2020-12-03 01:52

When I try to compile this code i get:

52 C:\\Dev-Cpp\\Projektyyy\\strategy\\Tiles.h invalid use of undefined type `struct tile_tree_apple\' 
46 C:\\Dev-Cpp\         


        
8条回答
  •  日久生厌
    2020-12-03 02:22

    In order for new T to compile, T must be a complete type. In your case, when you say new tile_tree_apple inside the definition of tile_tree::tick, tile_tree_apple is incomplete (it has been forward declared, but its definition is later in your file). Try moving the inline definitions of your functions to a separate source file, or at least move them after the class definitions.

    Something like:

    class A
    {
        void f1();
        void f2();
    };
    class B
    {
       void f3();
       void f4();
    };
    
    inline void A::f1() {...}
    inline void A::f2() {...}
    inline void B::f3() {...}
    inline void B::f4() {...}
    

    When you write your code this way, all references to A and B in these methods are guaranteed to refer to complete types, since there are no more forward references!

提交回复
热议问题