Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;继承和指针错误(初学者) 我是一个C++初学者,我在过去的4个月里一直在学习。我们有一个学校作业要做;使用我们从C++学到的东西创建一个基于文本的冒险游戏。今天我们复习了类、继承和指针,所以我想我应该练习使用类/继承_C++_Class_Header Files - Fatal编程技术网

C++;继承和指针错误(初学者) 我是一个C++初学者,我在过去的4个月里一直在学习。我们有一个学校作业要做;使用我们从C++学到的东西创建一个基于文本的冒险游戏。今天我们复习了类、继承和指针,所以我想我应该练习使用类/继承

C++;继承和指针错误(初学者) 我是一个C++初学者,我在过去的4个月里一直在学习。我们有一个学校作业要做;使用我们从C++学到的东西创建一个基于文本的冒险游戏。今天我们复习了类、继承和指针,所以我想我应该练习使用类/继承,c++,class,header-files,C++,Class,Header Files,到目前为止,我的代码是: Character.h(头文件) Battle.cpp(一切发生的地方) #包括 #包括 #包括“Character.h” 使用名称空间std; int main() { Character*Player=新角色(); 怪物*怪物1=新怪物(); cout我想你的问题可能是打字错误,你写了: cout << "Player's Health is: " << Player->SetHealth << endl <<

到目前为止,我的代码是:

Character.h(头文件)

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

#包括
#包括
#包括“Character.h”
使用名称空间std;
int main()
{
Character*Player=新角色();
怪物*怪物1=新怪物();

cout我想你的问题可能是打字错误,你写了:

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

希望这对您有所帮助,祝您今后在使用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
还有Character.cpp

#include "Character.h"

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

Character::~Character()
{
}

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

然后你可以用类似于
DrinkHealthPotion();
GetDamage();

我修复了你的o型:)的方法来更改它。谢谢你的帮助!一旦我输入
Player->GetHealth())
我收到一个错误,说:“GetHealth无法访问”,因为它在玩家和怪物中都是私有的。“啊,我没想到那么远,但是救援似乎正在进行中@Michal有一个答案,似乎涵盖了所有内容,同时还有一些非常有用的评论:)哈哈,谢谢你的帮助:)一旦我得到这个down我想我会对继承和指针有更好的理解。虽然我不认为我错得太远。虚拟对于
setHealth
来说似乎是杀伤力过大,因为它已经需要一个参数。它在
Monster
中得到了不同的实现,因此它必须是
virtual
。不同的是,我们不应该有一个y
setHealth
method(检查我答案的后半部分)。我想你没有领会我的意思。我知道你需要它在你的版本中是虚拟的,但我不知道他希望如何根据参数计算每个类的特定运行状况。例如,一个函数可能就足够了。
cout << "Player's Health is: " << Player->SetHealth << endl << endl;
cout << "Monster's Health is: " << Monster1->SetHealth << endl << endl;
cout << "Player's Health is: " << Player->GetHealth() << endl << endl;
cout << "Monster's Health is: " << Monster1->GetHealth() << endl << endl;
#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
#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;
}
#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
#include "Character.h"

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

Character::~Character()
{
}

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