2

I've a project in C++ with 2 classes.

MAIN.CPP:

#include <iostream>
#include <conio.h>
#include "Player.h"
#include "Enemy.h"

using namespace std;

int main()
{
const int startHealth = 100, startArmor = 50, startEnemyHealth = 70, startWeapon = 1;

Player *Mick = new Player(startHealth, startArmor, startWeapon);
Enemy *Mon = new Enemy(startEnemyHealth);

cout << "START!" << endl;
cout << Mick->Health << " : " << Mick->Armor << endl;

cout << "ENEMY ATTACKS!" << endl;
Mon->Attack(Mick);

cout << "DAMAGE TAKEN!" << endl;
cout << Mick->Health << " : " << Mick->Armor << endl;

cout << "YOU ATTACK!" << endl;
Mick->Attack(Mon);

cout << "ENEMY'S HEALTH!" << endl;
cout << Mon->Health << endl;

_getch();
return 0;
} 

PLAYER.H

#pragma once
#include "Enemy.h"

class Player
{
public:
int Health, Armor;
int Weapon;

public:
Player(const int _startHealth, const int _startArmor, const int         _startWeapon);
~Player();
void Attack(Enemy *_attackedEnemy);
};

ENEMY.H

#pragma once
#include "Player.h"

class Enemy
{
public:
float Speed;
int Damage;
int Health;

public:
Enemy(const int _startEnemyHealth);
~Enemy();
void Attack(Player *_attackedPlayer);
void Refresh(Enemy *_enemyToRefresh);
};

The errors that I get are these ones:

Meanwhile, these are the errors that CodeBlocks gives me:

Can someone help me with this issue?

RJFalconer
  • 10,890
  • 5
  • 51
  • 66
gedamial
  • 1,498
  • 1
  • 15
  • 30
  • you must forward declare the class, in player.h and enemy.h, as both includes each other but they wont be able to find the complete class definition. – Tejendra Jun 26 '15 at 09:22
  • possible duplicate of [When can I use a forward declaration?](http://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration) – Tejendra Jun 26 '15 at 09:32

1 Answers1

2

The problem is that your Player class refers to your Enemy class, which also refers to your Player class:

class Enemy
{
    void Attack(Player *_attackedPlayer);
}
class Player
{
    void Attack(Enemy *_attackedEnemy);
}

What you need is forward declaration, to inform the compiler that a particular class exists, without telling it any information about this class.

Here you can add the following line in the file Enemy.h, before the definition of the Enemy class:

class Player;

Look at this question to see what you can or cannot do with forward declarations.

Why you need it here, even with the relevant #include directives

An #include directive is basically an instruction for the preprocessor that tells it to replace the directive by the included file. The #pragma once directive ensures that the file won't be included more than once for each translation unit.

In Main.cpp, here's what is going on:

  • #include "Player.h": the file Player.h is included.
  • The first line of Player.h is #include "Enemy.h": the file Enemy.h is included.
  • The first line of Enemy.h is #include "Player.h": since the file Player.h has already been included, the directive is ignored.
  • Definition of Enemy
  • Definition of Player
  • Definition of the main function

As you can see, even with the includes, at the time of the definition of the class Enemy, the compiler doesn't know that a class Player exists yet. This is the reason why you absolutely need a forward declaration here.

Community
  • 1
  • 1
BlackDwarf
  • 2,070
  • 1
  • 17
  • 22
  • Thanks, really. Thanks very much!! But, can I ask you why forward declaration is needed, even though I have included the .h in both classes? – gedamial Jun 26 '15 at 10:03
  • I have a qustion, Is it true that the first letter of the class name should be capitalized ? Or it does not matter anymore? *(in php)* Tnx –  Jun 26 '15 at 11:13
  • @stack In C++ it is common practice to start class/struct/enum names with a capital letter, but nothing forces you to. As long as your naming conventions are consistent within a project it's fine IMHO. – BlackDwarf Jun 26 '15 at 11:16