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 couldn't find anything simmilar to my problem... Actually, I have more classes with parent "tile" and It was ok before...
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 computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.
A forward declaration tells the compiler about the existence of an entity before actually defining the entity. Forward declarations can also be used with other entity in C++, such as functions, variables and user-defined types.
The main rule is that you can only forward-declare classes whose memory layout (and thus member functions and data members) do not need to be known in the file you forward-declare it. This would rule out base classes and anything but classes used via references and pointers. Almost.
A struct (without a typedef) often needs to (or should) be with the keyword struct when used. struct A; // forward declaration void function( struct A *a ); // using the 'incomplete' type only as pointer. If you typedef your struct you can leave out the struct keyword.
Use forward declaration when possible.
Suppose you want to define a new class B
that uses objects of class A
.
B
only uses references or pointers to A
. Use forward declaration then you don't need to include <A.h>
. This will in turn speed a little bit the compilation.
class A ; class B { private: A* fPtrA ; public: void mymethod(const& A) const ; } ;
B
derives from A
or B
explicitely (or implicitely) uses objects of class A
. You then need to include <A.h>
#include <A.h> class B : public A { }; class C { private: A fA ; public: void mymethod(A par) ; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With