我試圖多載 << 以將類的受保護成員列印為字串,但是當我嘗試在另一個類中使用它時,std::cout << player2;我得到“0x7f60b0100”作為輸出。
“player2”是一個演員*,所以我不確定發生了什么。
class Actor {
private:
string type;
protected:
int health;
int damage;
vector<MoveType> moves;
public:
Actor(string type, int health): type{ type }, health{ health }{damage=0;}
virtual void Hit(int damage){health = health-damage;}
virtual void Heal(int amount){health= amount;}
const vector<MoveType>& GetMoves() const {return moves;}
bool IsDead() { return health <= 0; }
friend ostream& operator<<(ostream& out, const Actor& actor){
return (out << "DAMAGE DONE: " << actor.damage << "HEALTH: "<< actor.health);
}
};
uj5u.com熱心網友回復:
正如您所說,它是一個指向Actor實體的指標,這就是您列印的內容,即該指標的值。
您需要取消參考指標:
std::cout << *player2;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/366722.html
標籤:C 11
下一篇:為什么不呼叫移動建構式?[復制]
