C++ class forward declaration

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

    To do anything other than declare a pointer to an object, you need the full definition.

    The best solution is to move the implementation in a separate file.

    If you must keep this in a header, move the definition after both declarations:

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

    Important

    You have memory leaks:

    tile tile_tree::onDestroy() {return *new tile_grass;};
    

    will create an object on the heap, which you can't destroy afterwards, unless you do some ugly hacking. Also, your object will be sliced. Don't do this, return a pointer.

提交回复
热议问题