【发布时间】:2015-04-12 15:12:35
【问题描述】:
我是 C++ 新手,遇到以下问题:
我有一个父类,叫做 Creature:
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’
...
我不明白为什么我从未定义过静态成员却说不是静态成员?
【问题讨论】:
标签: c++ oop inheritance static