私はC++の初心者です。私は単純なユニットクラスと英雄クラスを持ち、ユニットクラスを継承しています。ヒーロークラスには2つの追加パラメータがありますが、コンストラクタは親のクラスパラメータに到達できません。ここ はunit.hppです:ここコンストラクタの継承されたクラスのメンバーにアクセスできません
#ifndef UNIT_HPP
#define UNIT_HPP
#include <string>
using namespace std;
class Unit
{
public:
unsigned short max_health = 100;
string name = "Dummy";
short health = 100;
short damage = 10;
bool isDead = 0;
Unit();
Unit(string, unsigned short, unsigned short);
};
#endif //UNIT_HPP
はunit.cppです:ここでは
#include <string>
#include <iostream>
#include "unit.hpp"
using namespace std;
Unit::Unit()
{
cout << "Dummy was created!" << endl;
};
Unit::Unit(string N, unsigned short HP, unsigned short AT):
max_health(HP),
name(N),
health(HP),
damage(AT)
{
cout << N << " was created!" << endl;
};
がhero.hppさ:
:、ここhero.cppは#ifndef HERO_HPP
#define HERO_HPP
#include <string>
#include "unit.hpp"
class Hero : public Unit
{
public:
unsigned short max_mana = 100;
string name = "The Brave Warrior";
short mana = 100;
Hero (string, unsigned short, unsigned short, unsigned short);
};
#endif //HERO_HPP
とfinalyです
#include <string>
#include "hero.hpp"
using namespace std;
Hero::Hero(string N, unsigned short HP, unsigned short MP, unsigned short AT):
max_health(HP),
max_mana(MP),
name(N),
health(HP),
mana(MP),
damage(AT)
{
cout << "The Legendary Hero, " << N << ", was born!" << endl;
}
ここにコンソールがありますut:
src/hero.cpp: In constructor ‘Hero::Hero(std::__cxx11::string, short unsigned int, short unsigned int, short unsigned int)’:
src/hero.cpp:10:5: error: class ‘Hero’ does not have any field named ‘max_health’
max_health(HP),
^
src/hero.cpp:13:5: error: class ‘Hero’ does not have any field named ‘health’
health(HP),
^
src/hero.cpp:15:5: error: class ‘Hero’ does not have any field named ‘damage’
damage(AT)
^
問題はどこですか?悪い英語を申し訳ありません。私は私に質問してほしい、私のための多くの新しい用語。前もって感謝します。
初期化リストから基本クラスコンストラクターを呼び出すか、派生コンストラクター本体のメンバーにアクセスできます。 –
* space savings *データ型を使用する場合は、 'uint8_t'や' uint16_t'のような 'uint#_t'型を使うべきです。これらのタイプは保証サイズです。 'short'型は少なくとも与えられた範囲を持つことが保証されています。より大きなものをサポートすることができます。例えば、32ビットの整数は 'short'として使用することができ、依然として言語の規則の範囲内にあります。 –