C ++类前向声明 [英] C++ class forward declaration

查看:113
本文介绍了C ++类前向声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译这个代码,我得到:

  52 C:\Dev-Cpp \Projektyyy \ strategy \Tiles.h无效使用未定义类型struct tile_tree_apple 
46 C:\Dev-Cpp \Projektyyy \strategy\ Tiles.h`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;};
};

我真的不知道该怎么办,我搜索的解决方案,但我couldnt找不到任何类似于我问题...事实上,我有更多的课程与父瓦片,它是确定之前...
Thanx任何帮助。



编辑: / p>

我决定将所有返回的类型改为指针,以避免内存泄漏,但现在我有:

  27 C:\Dev-Cpp \Projektyyy \strategy\Tiles.h ISO C ++禁止声明没有类型
的`tile'声明27 C:\Dev-Cpp\\ \\ Projektyyy \strategy\Tiles.h expected`;'beforetick

基本类,一切都是确定...每个函数在瓦片类返回*瓦有这个错误...



一些代码:

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


解决方案

为了 以编译, T 必须是完整类型。在你的情况下不是。尝试将函数的内联定义移动到单独的源文件,或至少移动它们的类定义。



像:

  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 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\Projektyyy\strategy\Tiles.h forward declaration of `struct tile_tree_apple' 

some part of my code:

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

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

EDIT:

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

27 C:\Dev-Cpp\Projektyyy\strategy\Tiles.h ISO C++ forbids declaration of `tile' with no type 
27 C:\Dev-Cpp\Projektyyy\strategy\Tiles.h expected `;' before "tick"

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

Some code:

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

解决方案

In order for new T to compile, T must be a complete type. In your case it isn't. Try moving the inline definitions of your functions to a separate source file, or at least move them afte r 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() {...}

这篇关于C ++类前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆