【发布时间】:2021-08-05 17:49:59
【问题描述】:
我开始学习运算符重载,起初它似乎很容易,但现在在尝试制作全局函数运算符时遇到访问私有成员的问题
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();
}
我没有看到那里有错误,但它一直在 Visual Studio 项目类成员上显示是私有的(无法访问 em),如果我在 player.hpp 和 item.h 标题顺序之间切换,我可以可以访问项目私有成员,然后我无法访问 player.hpp 私有成员
任何帮助将不胜感激。
【问题讨论】:
-
你声明了 4 个朋友。其中哪些不能访问私有成员?请发布确切的编译器错误。
-
您的
#include指令中有一个循环,player.hpp包括item.h和item.h包括player.hpp。这不行,你需要消除循环。 -
@S.M.正是那个运算符函数需要两个类,所以我在不同的类中声明为朋友
-
@n.'pronouns'm。请解释更多
-
规范的circular dependency dupe。在这种情况下,您可以在
player.hpp中转发声明item和/或在item.h中转发声明player。
标签: c++ visual-c++ private-members friend-function