Subtracting value from 2 integers in 2 classes

The title is confusing i know, i couldn't think of the right name. Anyway, i have 2 integers, both in 2 seperate classes in 2 seperate files. I have main.cpp, test_01.h, Test_01.cpp, Player.h and Player.cpp. I ahve a function which takes an integer (a) as a parameter (from Test_01.h and Test_01.cpp). a sets another integer (attackPower) from the same class equal to a. The integer (health) in the other class (Player.h and Player.cpp), is then equal to health -= attackPower. But instead of giving me the right answer 75 (health = 100 and a is set to 25 when i call the function) it gives me the answer.

Thank you in advance.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "Test_01.h"
#include "Player.h"

using namespace std;

int main()
{
    Player p;

    Test_01 t;

    t.setAttackPower(25);

    cout << p.health << endl;

    return 0;
}


Test_01.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef TEST_01_H
#define TEST_01_H
#include "Player.h"



class Test_01
{
    public:
        Player p;
        Test_01();
        void setAttackPower(int a);
        int attackPower;

        int getHealth();
};

#endif // TEST_01_H


Test_01.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Test_01.h"
#include <iostream>

using namespace std;

Test_01::Test_01()
{

}

void Test_01::setAttackPower(int a)
{
    attackPower = a;
    p.health -= attackPower;
}


Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef PLAYER_H
#define PLAYER_H


class Player
{
    public:
        Player();
        int health;
};

#endif // PLAYER_H



Player.cpp
1
2
3
4
5
6
7
8
9
#include "Player.h"

Player::Player()
{

}

int health = 100;
The "p" in main and the "p" in your class aren't the same thing.

I would be tempted to give the player class a TakeDamage function:
1
2
3
4
void Player::TakeDamage( int damage )
{
   health -= damage;
}


Then maybe have an Attack function for the other class, passing in a target.
1
2
3
4
void Test01::Attack( Player &target )
{
   target.TakeDamage( attackPower );
}


Ideally, a better way to do this would be to have a GameObject type or something like that, that already has the Attack and TakeDamage functions. Then you could inherit from that class. This way, you could pass in a reference to a GameObject, rather than a Player in the Attack function.
Last edited on
I figured it out with inspiration from iHutch105 (thank you). I made a void exactly the same as TakeDamage Above. I then made the void above as an int with an int parameter of your choice. it returns the int of your choice. Then you call take damage and for the parameters you put attack with the int of your choice.

Calling the functions
1
2
3
4
5
6

    Player p;

    Enemy e;

    p.takeDamage(e.Attack(20));


TakeDamage
1
2
3
4
5
6
7

health = 100;

void Player::takeDamage(int damage)
{
    health -= damage;
}


Attack
1
2
3
4
int Enemy::Attack(attackPower)
{
   return attackPower;
}


Output = 80.
Topic archived. No new replies allowed.