C++ class forward declaration

后端 未结 8 599
春和景丽
春和景丽 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:17

    class tile_tree_apple should be defined in a separate .h file.

    tta.h:
    #include "tile.h"
    
    class tile_tree_apple : public tile
    {
          public:
              tile onDestroy() {return *new tile_grass;};
              tile tick() {if (rand()%20==0) return *new tile_tree;};
              void onCreate() {health=rand()%5+4; type=TILET_TREE_APPLE;}; 
              tile onUse() {return *new tile_tree;};       
    };
    
    file tt.h
    #include "tile.h"
    
    class tile_tree : public tile
    {
          public:
              tile onDestroy() {return *new tile_grass;};
              tile tick() {if (rand()%20==0) return *new tile_tree_apple;};
              void onCreate() {health=rand()%5+4; type=TILET_TREE;};        
    };
    

    another thing: returning a tile and not a tile reference is not a good idea, unless a tile is a primitive or very "small" type.

提交回复
热议问题