久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ 類前向聲明

C++ class forward declaration(C++ 類前向聲明)
本文介紹了C++ 類前向聲明的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當我嘗試編譯這段代碼時,我得到:

When I try to compile this code, I get:

52 C:Dev-CppProjektyyystrategyTiles.h invalid use of undefined type `struct tile_tree_apple' 
46 C:Dev-CppProjektyyystrategyTiles.h forward declaration of `struct tile_tree_apple' 

我的部分代碼:

class tile_tree_apple;

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;};        
};

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;};       
};

我真的不知道該怎么辦,我搜索了解決方案,但找不到任何與我的問題類似的東西...實際上,我有更多與父級tile"相關的課程.之前還可以...

I dont really know what to do, I searched for the solution but I couldn't find anything simmilar to my problem... Actually, I have more classes with parent "tile" and It was ok before...

我決定將所有返回的類型更改為指針以避免內存泄漏,但現在我得到了:

I decided to change all returned types to pointers to avoid memory leaks, but now I got:

27 C:Dev-CppProjektyyystrategyTiles.h ISO C++ forbids declaration of `tile' with no type 
27 C:Dev-CppProjektyyystrategyTiles.h expected `;' before "tick"

僅在基類中,其他一切正常... tile 類中返回 *tile 的每個函數都有此錯誤...

Its only in base class, everything else is ok... Every function in tile class which return *tile has this error...

一些代碼:

class tile
{
      public:
          double health;
          tile_type type;
          *tile takeDamage(int ammount) {return this;};
          *tile onDestroy() {return this;};
          *tile onUse() {return this;};
          *tile tick() {return this};
          virtual void onCreate() {};
};

推薦答案

new T 要編譯,T 必須是完整類型.在您的情況下,當您在 tile_tree::tick 的定義中說 new tile_tree_apple 時,tile_tree_apple 是不完整的(它已被前向聲明,但是它的定義稍后在您的文件中).嘗試將函數的內聯定義移動到單獨的源文件中,或者至少將它們移動到類定義之后.

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.

類似于:

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() {...}

當您以這種方式編寫代碼時,這些方法中對 A 和 B 的所有引用都保證引用完整類型,因為不再有前向引用!

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!

這篇關于C++ 類前向聲明的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 天天干狠狠 | 欧洲精品码一区二区三区免费看 | 日本免费视频在线观看 | 亚洲精品一二三区 | 日本涩涩网 | 做a视频 | 国产精品福利视频 | 国产一区二区日韩 | 亚洲h在线观看 | 超碰97免费观看 | 午夜精品影院 | 一区二区视频在线 | 羞羞网站免费观看 | 日韩成人在线视频 | 91网站视频在线观看 | 午夜a√ | av网站免费看 | 精品三级在线观看 | 日本午夜视频 | 免费在线毛片 | 91精品国产日韩91久久久久久 | 在线欧美视频 | 三级视频网站 | 亚洲日本乱码在线观看 | 亚洲视频在线观看 | 狠狠干美女 | 午夜爱爱网| 日韩色在线 | 亚洲免费在线观看 | 日韩一区二区三区视频 | 亚洲一区二区精品视频在线观看 | 中文字幕亚洲在线 | 999热视频 | 中文字幕第一页在线 | 日韩中文字幕 | 亚洲成人黄色 | 国产在线观看网站 | 日韩精品国产精品 | 久久av一区 | 亚洲精品久久久久久国产精华液 | 在线视频99 |