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.