gpt4 book ai didi

c++ - 使用未定义的 'Player' 和 'player' 使用未定义的 'Player' 错误

转载 作者:行者123 更新时间:2023-11-28 05:21:32 27 4
gpt4 key购买 nike

class Skeleton {
public:
void attack(Player player) {
if (canAttackSkeleton()) {
player.setHealth(player.getHealth() - _attackDamage); //C2027 and C2228 for setHealth() and getHealth()
return;
}
}
void getAttacked(Player player) {
_health -= player.getAttackDamage(); //C2027 and C2228 for getAttackDamage()
}


private:
float _attackDamage = 100.0f;
};

class Player{
public:
void attack(Skeleton skeleton) {
if (canAttackPlayer()) {
skeleton.setHealth(skeleton.getHealth() - _attackDamage);
return;
}
}
void getAttacked(Skeleton skeleton) {
_health -= skeleton.getAttackDamage();
}


private:
float _attackDamage = 100.0f;
};

这是我的代码加上主要功能

基本上我得到“use of undefined 'Player'”错误代码 C2027。此外,对于 .setHealth.getHealth.getAttackDamage,我收到错误消息“必须具有类/结构/union ”,错误代码为 C2228

我做错了什么?

最佳答案

Undefined表示编译器找不到Player的定义。如果您切换顺序,那么您将收到未定义骨架的错误。你应该考虑有一个字符类。

#include<iostream>
class Character {
public:
Character() : health(100), attackDamage(10) {}
virtual void attack(Character& other) {
other.getAttacked(*this);
}
virtual void getAttacked(Character& other) {
this->health -= other.getAttackDamage();
}
virtual int getHealth() const { return health; }
virtual void setHealth(int Health) { health = Health; }
virtual int getAttackDamage() const { return attackDamage; }
virtual void setAttackDamage(int AttackDamage) { attackDamage = AttackDamage; }

protected:
int health;
int attackDamage;
};

class Player : public Character {

};

class Skeleton : public Character {

};

int main(int argc, char* argv[]) {

Player player;
Skeleton skeleton;
std::cout << "Before attack...\n\nPlayer Health: " << player.getHealth() << std::endl << "Skeleton Health: " << skeleton.getHealth() << std::endl;

player.attack(skeleton);
std::cout << "After attack...\n\nPlayer Health: " << player.getHealth() << std::endl << "Skeleton Health: " << skeleton.getHealth() << std::endl;

player.setAttackDamage(50);
std::cout << "Updated player attack to 50" << std::endl;
player.attack(skeleton);

std::cout << "After second attack with 50 attack damage...\n\nPlayer Health: " << player.getHealth() << std::endl << "Skeleton Health: " << skeleton.getHealth() << std::endl;

return 0;
}

关于c++ - 使用未定义的 'Player' 和 'player' 使用未定义的 'Player' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41403698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com