问题描述
当我尝试编译此代码时,我得到:
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
必须是完整类型。在你的情况下,当你在 tile_tree :: tick
,的定义中说
是不完整的(它已被向前声明,但其定义在您的文件中较晚)。尝试将函数的内联定义移动到单独的源文件,或至少在类定义后移动它们。 new tile_tree_apple
tile_tree_apple
类似:
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 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, 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.
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() {...}
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 ++类前向声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!