Error previous definition of 'class Player' in C ++

0

Hello everyone I have a problem with my code compiled well I do not remember that I moved it and it gives me an error. Here I leave my code.

#include <iostream>
using namespace std;

#include "Player.h"
#include "Enemy.h"

int main(){
   Player player1( 3, 10 );
   Enemy enemy1( 4, 10 );

   while( player1.receiveDamage( enemy1.inflictDamage() ) > 0 
      && enemy1.receiveDamage( player1.inflictDamage() ) > 0 ) {
      cout << "Player 1: ";
      player1.printLife();

      cout << "Enemy: ";
      enemy1.printLife();
   }
 }

Player.h

class Player{
public:
    Player( int, int );
    int inflictDamage();
    int receiveDamage( int );
    void printLife();
private:
    int attack;
    int life;
};

Player.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

#include "Player.h"

Player::Player( int ap, int lp ){
   attack = ap;
   life = lp;

   srand( time( 0 ) );
}

int Player::inflictDamage(){
   int damage = 0;
   int dice = 1 + rand() % 6;

   if( dice > 3 )
      damage = attack;
   else
      damage = 0;

  return damage;
}

int Player::receiveDamage( int damage ){
   return life -= damage;
}

void Player::printLife(){
   cout << life << endl;

}

Enemy.h

#include "Player.h"

class Enemy : public Player{
   public:
       Enemy( int, int );
};

Enemy.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

#include "Enemy.h"

Enemy::Enemy( int ap, int lp ) : Player( ap, lp ){

   srand( time( 0 ) );
}

These are the errors that come to me:

  

[Error] redefinition of 'class Player'   [Error] previous definition of 'class Player'

Edited:

I understand about the guards but I need the two player and enemy objects in the main to "fight", I think I'm using the inheritance wrong and now I get another error.

pelea.cpp:(.text+0xc1): undefined reference to 'Player::receiveDamage(int)'

is the same error with all the functions of Player.cpp.

Pd. I already put the #ifndef #define where I was missing.

    
asked by Zombie Jew Cosmic 10.04.2017 в 20:04
source

1 answer

3

You have included Player.hpp twice. main.cpp includes Player.hpp , and Enemy.hpp , and Enermy.hpp include a Player.hpp again, which means, that main.cpp is seeing Player.hpp twice.

What you have not got are guards:

// Player.hpp
#ifndef PLAYER_HPP
#define PLAYER_HPP

// El nombre elegido para el ifndef/define no tiene porqué coincidir
// con el nombre del fichero. Simplemente, no debe de coincidir con
// ningún otro define que tengas en el programa. La manera más
// sencilla, es hacer que el nombre del define coincida con el del 
// fichero, dado que no vas a tener dos ficheros iguales, no vas a
// tener dos defines iguales. El uso de mayúsculas es sencillamente
// costumbre. Los define se suelen poner en mayúscula, y en el código
// real, no se suele utilizar ninguna variable ni función que esté
// entera en mayúscula. De esta forma, con solo ver el nombre, sabes
// si se trata de un define o no.

class Player{
public:
    Player( int, int );
    int inflictDamage();
    int receiveDamage( int );
    void printLife();
private:
    int attack;
    int life;
};

#endif

This pair of preprocessor directives are called guards , because they protect the file (if it is not included twice). If PLAYER_HPP is not defined (the first time it is included is not defined), it is defined. The second time it is included, when it is already defined, the if is skipped, and the code from there to the #endif is not included.

All headers, any file .h , .hpp , etc, any file that is going to be included in another site, should have guards as a healthy custom.

The .cpp and files that are not included, do not need guards, since they are not included, it's worth the redundancy.

The same since doing with Enemy.hpp , of course.

    
answered by 10.04.2017 / 20:30
source