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

C++ 類前向聲明

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

問題描述

當(dāng)我嘗試編譯這段代碼時,我得到:

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"相關(guān)的課程.之前還可以...

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...

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

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 的每個函數(shù)都有此錯誤...

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 必須是完整類型.在您的情況下,當(dāng)您在 tile_tree::tick 的定義中說 new tile_tree_apple 時,tile_tree_apple 是不完整的(它已被前向聲明,但是它的定義稍后在您的文件中).嘗試將函數(shù)的內(nèi)聯(lián)定義移動到單獨的源文件中,或者至少將它們移動到類定義之后.

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

當(dāng)您以這種方式編寫代碼時,這些方法中對 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!

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

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 国产亚洲精品美女久久久久久久久久 | 国产99久久精品一区二区永久免费 | 欧美极品视频 | 激情av | 国产精品国色综合久久 | 99国产精品久久久久老师 | 国产成人精品免费视频大全最热 | 中文字幕综合 | 久久久久久久久久久久久久av | 国产一区二区欧美 | 亚洲久久 | 一级片成人 | 日本三级在线 | 免费在线观看黄网站 | 羞羞羞视频 | 久久久久国产一区二区三区 | 欧美一级三级在线观看 | 欧美在线a | 日本一区二区三区免费观看 | av在线伊人| 色婷婷一区二区三区四区 | 久久av一区 | 中文字幕国产在线 | 免费骚视频 | 亚洲夜射 | 亚洲电影一区二区三区 | 亚洲日本免费 | 亚洲综合三区 | 成年人黄色小视频 | 插插插干干干 | 国产欧美一区二区久久性色99 | 欧美99 | 精品中文字幕一区二区 | 日韩在线一区二区三区 | 亚洲成人精品一区 | h视频免费在线观看 | 亚洲精品国产成人 | 91在线影院| 特级特黄特色的免费大片 | 久国产视频 | 久久精品国产亚洲 |