gpt4 book ai didi

C++ 继承和指针错误(初学者)

转载 作者:行者123 更新时间:2023-11-28 07:14:39 25 4
gpt4 key购买 nike

我是 C++ 的初学者,过去 4 个月我一直在学习。我们有一项学校作业要做;使用我们迄今为止从 C++ 中学到的知识创建一个基于文本的冒险游戏。我们今天讨论了类、继承和指针,所以我想我应该练习使用类/继承。

到目前为止我的代码是:

Character.h(头文件)

#ifndef CHARACTER_H
#define CHARACTER_H
#include <cstdlib>

class Character
{
private:
int Health;

public:
Character();
~Character();

int GetHealth() {return Health;}
void SetHealth(int newHealth) {Health = newHealth;}


};

class Monster:public Character
{
int GetHealth() {return Health;}
void SetHealth(int newHealth) {Health = rand()% 50+100;}
}

#endif

Character.cpp

#include "Character.h"

Character::Character()
{
Health = 100;
}

Character::~Character()
{
}

Battle.cpp(一切发生的地方)

#include <iostream>
#include <cstdlib>
#include "Character.h"
using namespace std;

int main()
{
Character* Player = new Character();
Monster* Monster1 = new Monster();

cout << "Player's Health is: " << Player->SetHealth << endl << endl;
cout << "Monster's Health is: " << Monster1->SetHealth << endl << endl;
}

让我解释一下我要做什么...

我只是想让程序显示玩家和怪物的生命值。

在角色头文件中,“角色”类是游戏中每个角色的基础。然后我获取并设置类(class)的健康状况。然后我试图创建一个从“角色”类派生的名为“怪物”的子类。与标准角色类别相比,这将具有不同的健康状况,但我不确定该怎么做。我想让生命值随机生成在 50 到 100 之间。然后在“Battle.cpp”文件中,我试图创建一个名为“玩家”的新角色,其中包括默认的生命值(即 100)和一个新怪物称为'Monster1'。然后我想显示两个角色的健康状况。我还打算创造多个具有不同健康状况的怪物。

当我尝试运行该程序时,我似乎收到大量错误提示“请参阅 Character::Health 声明”。

我不太确定我做错了什么,因为我对 C++ 还是很陌生,并且仍在努力了解指针和继承的概念。

最佳答案

让我在代码中回答:

#ifndef CHARACTER_H
#define CHARACTER_H
#include <cstdlib>

class Character
{
// this property will be used in the derived classes, we need protected instead of private
protected:
int Health;

public:
Character();
~Character();

// This method is to be repeated in the derived class, and publicly accessible
// (not only inside the class, but also outside (like you do in main())
public:
int GetHealth() {return Health;}
// This method needs to be redefined in Monster, that's a candidate for a virtual method
virtual void SetHealth(int newHealth) {Health = newHealth;}


};

class Monster:public Character
{
// You don't need this anymore - the GetHealth from class Character is accessible
// int GetHealth() {return Health;}

// you override a method - write a different version. Use virtual to note that
virtual void SetHealth(int newHealth) {Health = rand()% 50+100;}
}; // lacked a semi-colon here :)

#endif

还有Battle.cpp的代码

#include <iostream>
#include <cstdlib>
#include "Character.h"

using namespace std;

int main()
{
Character* Player = new Character();
Monster* Monster1 = new Monster();

// The moment you cout, you need to provide a value (a method that returns something).
// You want to "GetHealth()" in order to show it :)
cout << "Player's Health is: " << Player->GetHealth() << endl << endl;
cout << "Monster's Health is: " << Monster1->GetHealth() << endl << endl;
}

我也不喜欢你在类里面对 Health 所做的事情。首先,您将其声明为 private - 因此任何人都无法以未经授权的方式更改它。然后声明一个简单的 SetHelth(),它允许您对值执行任何操作。

相反,您可以在构造函数中设置初始值:

#ifndef CHARACTER_H
#define CHARACTER_H
#include <cstdlib>

class Character
{
// this property will be used in the derived classes, we need protected instead of private
protected:
int Health;

public:
Character();
~Character();

// This method is to be repeated in the derived class, and publicly accessible
// (not only inside the class, but also outside (like you do in main())
public:
int GetHealth() {return Health;}
};

class Monster:public Character
{
// You don't need this anymore - the GetHealth from class Character is accessible
// int GetHealth() {return Health;}

public:
Monster();
}; // lacked a semi-colon here :)

#endif

和 Character.cpp

#include "Character.h"

Character::Character()
{
Health = 100;
}

Character::~Character()
{
}

Monster::Monster()
{
Health = rand()% 50+100;
}

然后您可以使用 DrinkHealthPotion();GetDamage(); 等方法更改它

关于C++ 继承和指针错误(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436039/

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