i started learning about operators overloading, at first it seem to easy, but now am having a problem accessing private member when try to make a global funtion operator
player.hpp
#ifndef _PLAYER_HPP_
#define _PLAYER_HPP_
#include <iostream>
#include <string>
#include "item.h"
class player
{
friend player operator+(player& obj, player& tem);
static int numPlayer;
float *health;
int mspeed;
int damage;
int xp;
std::string name;
public:
// Constructors
player(std::string = "player", float _health = 100, int _xp = 0);
// Copy Constructor
player(const player& obj);
// Move Constructor
player(player&& obj);
// Functions
void display();
// Friends functions
friend void test(player user);
friend player operator+(player &&obj, const item &tem);
// Diconstructors
~player();
};
#endif // _PLAYER_HPP_
player.cpp
#include "player.hpp"
#include "item.h"
#include <iostream>
#include <cstring>
#include <string>
int player::numPlayer = 0;
// Constructors
player::player(std::string _name, float _health, int _xp) {
numPlayer++;
this->health = new float;
*this->health = _health;
this->xp = _xp;
this->name = _name;
std::cout << "constructor for " << this->name << std::endl;
}
// Copy constructors
player::player(const player& obj) {
this->health = new float;
*this->health = *obj.health;
this->xp = obj.xp;
this->name = obj.name;
std::cout << "copy constructor for " << this->name << std::endl;
}
// Move Constructors
player::player(player&& obj) {
this->damage = 60;
this->mspeed = 50;
this->health = obj.health;
this->xp = obj.xp;
this->name = obj.name;
obj.health = nullptr;
std::cout << "Move constructor for " << this->name << std::endl;
}
void player::display() {
std::cout << "========================" << std::endl
<< this->name << std::endl
<< *this->health << std::endl
<< this->xp << std::endl
<< this->damage << std::endl
<< this->mspeed << std::endl;
}
player::~player() {
delete[] health;
std::cout << "distruction for: " << name << std::endl;
}
void test(player user) {
std::cout << user.name << std::endl;
}
player operator+(player&& obj, const item& tem) {
*obj.health += tem.health;
obj.damage += tem.damage;
obj.mspeed += tem.ms;
return obj;
}
item.h
#ifndef _ITEM_H_
#define _ITEM_H_
#include <iostream>
#include <string>
#include "player.hpp"
class item {
int damage; // Bonus damage
int health; // Bonus health
int ms; // Bonus Movement speed
std::string name; // item name
public:
//constructor
item(std::string name, int _damage = 0, int _health = 0, int _ms = 0)
: name {name}, damage {_damage}, health{_health}, ms {_ms}{}
friend player operator+(player &&obj,const item &tem);
};
#endif // _ITEM_
Main.cpp
#include <iostream>
#include <string>
#include "player.hpp"
#include "item.h"
player operator+(player&& obj, const item& tem);
void test(player user);
void main(int args, char* argv) {
player a("YASOU96");
item deathSword("death Sword", 150, 0, 20);
a.display();
a = a + deathSword;
a.display();
}
i dont see that there is a mistake there but it keep showing on visual studio item class member are private (can't access em), nd if i switch between the player.hpp and item.h header order, i can have access to item private member, nd then i lose access on player.hpp private member
Any help would be appreciated.