我是C ++的新手。我有简单的Unit类和英雄类,它继承了Unit类。 Hero类有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:
#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
最后,这里是hero.cpp:
#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;
}
这是控制台输出:
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)
^
问题出在哪里?抱歉英语不好。我希望我提出的问题是正确的,对我来说有这么多新条款。先感谢您。
答案 0 :(得分:3)
您的基类应负责通常通过构造函数方法初始化其变量。
这:
unsigned short max_health = 100;
string name = "Dummy";
short health = 100;
short damage = 10;
bool isDead = 0;
看起来不是犹太人。应该在构造函数中初始化这些成员:
Unit::Unit()
: max_health(100),
name("Dummy"),
health(100),
damage(10),
isDead(false)
{ ; }
此外,对于bool
个变量,您应该使用true
或false
,而不是数字。
编辑1:重复的成员名称
您的子类应避免使用与基类相同的变量名。
英雄行:
string name;
隐藏或隐藏基类成员:
string name;
如果您希望保持此约定,则应使用范围解析运算符 ::
告诉编译器您指的是哪个成员:
Hero::name = "Hercules"; // Assign member in Hero class
Unit::name = "Person"; // Assign to member in Unit class.
答案 1 :(得分:0)
在Unit的构造函数中初始化Unit的成员(如果你只想从Hero中调用它,可能是受保护的成员),以及Hero在Hero中的成员。这就是初始化程序列表的工作原理。或者,您可以在Hero的ctor之间使用它们,但不建议这样做。
答案 2 :(得分:0)
C ++不允许你初始化基类&#39;来自子类的成员&#39;初始化列表。
待办事项
Hero::Hero(string N, unsigned short HP, unsigned short MP, unsigned short AT):
Unit(N, HP, AT), // initializes the base class' members
max_mana(MP),
name(N),
mana(MP),
{
// but you could override the base class' members here
isDead = true;
cout << "The Legendary Hero, " << N << ", was zombified!" << endl;
}
此外,您还有一位名为&#39; name&#39;在Unit和Hero中,你可能不想删除或重命名其中一个。
答案 3 :(得分:0)
您在Hero.cpp构造函数中尝试执行的操作是初始化基类的成员。这对于基类本身来说听起来非常糟糕!实际上,到达分号时,也就是说,基类构造函数Unit()已经被调用(因此,它所包含的内容已经被初始化)。所以,如果你要从Unit中删除Unit(),你会得到一个编译错误,因为另一个构造函数有参数。那些你必须明确指定的那样:
Hero::Hero(string N, unsigned short HP, unsigned short MP, unsigned short AT):
Unit(HP, N, HP, AT),
max_mana(MP),
mana(MP),
{}
请注意,这次,根本没有调用Unit() - 只有其他的contsructor是 - 显式。