1
的静态成员,我是一个新手,C++和具有以下问题:儿童类没有父
我有一个父类,称为怪物:像这样
class Creature
{
public:
bool isActive;
std::string name;
int attackNumOfDice, attackNumOfSides,
defenseNumOfDice, defenseNumOfSides;
int armor, strength;
Creature();
//two virtual functions determine the damage points
virtual int attack();
virtual int defend();
void halveAttackDice();
void setStrength(int toSet);
void setStatus(bool activity);
};
和5子类:
.h文件中:
class Child : public Creature
{
int attack();
int defend();
}
实现文件:
int Child::isActive = true;
std::string Child::name = "";
int Child::attackNumOfDice = 2;
...
int Child::attack()
{
...
}
intChild::defend()
{
...}
然而,当我尝试编译这样我得到所有5个子类相同的错误:
child.cpp:6: error: ‘bool Child::isActive’ is not a static member of ‘class Child’
child.cpp:7: error: ‘std::string Child::name’ is not a static member of ‘class Child’
child.cpp:8: error: ‘int Child::attackNumOfDice’ is not a static member of ‘class Child’
...
我不明白为什么说不是一个静态的成员时,我从来没有一个定义?
谢谢...有没有办法只使用父类中的构造函数呢?提示说应该只有一个构造函数 – 2015-02-12 02:18:16
yes ......你可以使用这个sintax:Child :: Child():Creature(){}从子类的构造函数调用父构造函数。您只需将您的子类构造函数留空,并且它将运行父构造函数中存在的代码。 – 2015-02-12 02:24:13
如果您尝试在构造函数中初始化变量,则使用构造函数体中的构造函数初始化程序列表,即赋值语句的intead。 – 2015-02-12 03:26:21