我正在编写一个基本的C ++控制台游戏。我有一个基类Character
,该基类由每个角色(玩家角色,敌人,NPC等)继承。为了管理每个角色的库存,角色会将游戏中的每种武器作为属性(然后是当前武器的属性:cur_weap
)。
我有一个Character
类的默认构造函数(某些值必须由子类重新初始化,因为抽象地定义它们是不明智的)。尽管我的项目构建没有错误,但是Intellisense在说:
“ Character :: Character()”没有为引用成员“ Character :: fist”提供初始化程序
(fist
是Fist : public Weapon
类的对象)
但是,您可能会猜到,该值是(我认为)已初始化。这是一些代码:
#include "Weapons.h" // Header for all weapon-realted classes
#define make_weap(_Ty) *std::unique_ptr<_Ty>(new _Ty)
class Character
{
protected:
/* ...Other class attribs here... */
Weapon& cur_weap;
Weapon& fist, sword, ... ; // And so on
public:
// Empty constructor
Character()
{ // (Error shows on this curly brace)
/* ...Define other attribs... */
fist = make_weap(Fist);
sword = make_weap(Sword);
// And so on...
cur_weap = fist;
}
};
#undef make_weap
对于某些情况,这是Weapon
构造函数和Fist
类的一部分:
/* WeaponType is just for setting attribs like whether
the weapon is "Melee", etc. Also sets ammo for a given
type. Has no methods besides constructor */
class Weapon : public WeaponType
{
public:
std::string name;
double damage, cost;
bool owned;
// Default constructor (WeaponType attribs must be redefined)
Weapon() : WeaponType(0)
{
name = "";
damage = cost = 0.0;
owned = false;
}
// Empty constructor
Weapon(unsigned classID) : WeaponType(classID)
{
/* (Same as above constructor) */
}
};
struct Fist : public Weapon
{
Fist() : Weapon(0) // 0 is classID for melee weapons
{
name = "Fist";
damage = 2;
cost = 0; // 0 because it's the starting weapon for any character
owned = true;
}
};
我是大学一年级学生(在CS中),所以我对继承和多态性有些陌生。随意评论我对这些事情的任何好/不好的编码实践。 (注意:这是一个个人项目,而不是一项任务)
我的主要问题是:Intellisense在说什么值得注意?如果是这样,它到底在说什么,为什么该项目还在建设中?如何修复代码以使Intellisense停止发出此警告?