Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ class forward declaration

Tags:

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() {}; }; 
like image 364
noisy cat Avatar asked Feb 02 '12 20:02

noisy cat


People also ask

What is a forward declaration in C?

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.

What is the purpose of forward class declaration?

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.

What can be forward declared?

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.

What is forward declaration of struct?

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.


1 Answers

Use forward declaration when possible.

Suppose you want to define a new class B that uses objects of class A.

  1. 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 ; } ; 
  2. 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) ;    } 
like image 50
kumaran Avatar answered Oct 05 '22 01:10

kumaran