1

The code is really simple (but I am a newbie so I have no idea what I am doing wrong) :

#include<iostream>
#include<string>

void PrintEntity(Entity* e);

class Entity
{
  public:
      int x,y;


      Entity(int x, int y)
      {

         Entity* e= this;
         e-> x=x;
         this->y=y;

         PrintEntity(this);

      }

  };

void PrintEntity(Entity* e)
  {
    // *Do stuff*
  }

int main()
  {

     return 0;
  }

My understanding of the error is that I cannot declare the function PrintEntity before of the class Entity. But even if I would declare the function below the class it would be a problem since in the Constructor I am calling the function PrintEntity.

So I am quite stuck . Can anybody explain to me what I am doing wrong please?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335

2 Answers2

2

Declare the function before the class definition like

void PrintEntity( class Entity* e);

using the elaborated type specifier.

Otherwise the compiler does not know what is Entity.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The compiler reads your file from the top down.

When it encounters void PrintEntity(Entity * e);, it must determine whether Entity * e is a formal parameter (making this a function declaration) or a multiplication (making this a variable declaration with initialiser).

Since the compiler is completely unaware of a type called "Entity", it decides that this must be a variable declaration, and a variable cannot have type void.

The solution is to declare the class before the function prototype, either separately:

class Entity;
void PrintEntity(Entity* e);

or directly in the function declaration:

void PrintEntity(class Entity* e);
molbdnilo
  • 64,751
  • 3
  • 43
  • 82