将枚举传递给不同文件中的对象
Passing Enums Into Objects in Different Files
所以我目前正在开发一个基于文本的角色扮演游戏,我 运行 遇到了一个奇怪的问题。在编写武器代码时,我选择使用枚举来表示武器的类型和稀有性。我已经为 Weapon
class 编写了所有程序;然而,当我尝试创建一个 Weapon
对象时,我收到一个与我的枚举有关的错误 -- error: 'common' is not a type
。相关代码如下:
在Enum_Weapon.h
中:
#ifndef ENUM_WEAPON_H_INCLUDED
#define ENUM_WEAPON_H_INCLUDED
enum rarity{common, uncommon, rare, epic, legendary};
enum weaponType{axe, bow, crossbow, dagger, gun, mace,
polearm, stave, sword, wand, thrown};
#endif // ENUM_WEAPON_H_INCLUDED
并且在 Weapon.h
中:
#ifndef WEAPON_H
#define WEAPON_H
#include "Item.h"
#include "Enum_Weapon.h"
class Weapon : public Item{
public:
Weapon();
Weapon(rarity r, weaponType t, std::string nam, int minDam,
int maxDam, int stamina = 0, int strength = 0,
int agility = 0, int intellect = 0);
当然,代码还在继续;但这是与我的错误相关的所有代码。最后,当我尝试创建一个 Weapon
对象时,出现错误:
#ifndef LISTOFWEAPONS_H
#define LISTOFWEAPONS_H
#include "Weapon.h"
#include "Enum_Weapon.h"
class ListOfWeapons
{
public:
ListOfWeapons();
protected:
private:
Weapon worn_greatsword(common, sword, "Worn Greatsword", 1, 2);
};
#endif // LISTOFWEAPONS_H
sword
枚举也会发生同样的错误。我已经研究了这个问题,但找不到与我遇到的问题类似的任何内容。非常感谢任何帮助!
你的武器属性是函数声明,不是变量定义。您必须在构造函数中传入默认值。
class ListOfWeapons
{
public:
ListOfWeapons() :
worn_greatsword(common, sword, "Worn Greatsword", 1, 2)
{
//...constructor stuff
}
protected:
private:
//function decl
//Weapon worn_greatsword(common, sword, "Worn Greatsword", 1, 2);
Weapon worn_greatsword;
};
所以我目前正在开发一个基于文本的角色扮演游戏,我 运行 遇到了一个奇怪的问题。在编写武器代码时,我选择使用枚举来表示武器的类型和稀有性。我已经为 Weapon
class 编写了所有程序;然而,当我尝试创建一个 Weapon
对象时,我收到一个与我的枚举有关的错误 -- error: 'common' is not a type
。相关代码如下:
在Enum_Weapon.h
中:
#ifndef ENUM_WEAPON_H_INCLUDED
#define ENUM_WEAPON_H_INCLUDED
enum rarity{common, uncommon, rare, epic, legendary};
enum weaponType{axe, bow, crossbow, dagger, gun, mace,
polearm, stave, sword, wand, thrown};
#endif // ENUM_WEAPON_H_INCLUDED
并且在 Weapon.h
中:
#ifndef WEAPON_H
#define WEAPON_H
#include "Item.h"
#include "Enum_Weapon.h"
class Weapon : public Item{
public:
Weapon();
Weapon(rarity r, weaponType t, std::string nam, int minDam,
int maxDam, int stamina = 0, int strength = 0,
int agility = 0, int intellect = 0);
当然,代码还在继续;但这是与我的错误相关的所有代码。最后,当我尝试创建一个 Weapon
对象时,出现错误:
#ifndef LISTOFWEAPONS_H
#define LISTOFWEAPONS_H
#include "Weapon.h"
#include "Enum_Weapon.h"
class ListOfWeapons
{
public:
ListOfWeapons();
protected:
private:
Weapon worn_greatsword(common, sword, "Worn Greatsword", 1, 2);
};
#endif // LISTOFWEAPONS_H
sword
枚举也会发生同样的错误。我已经研究了这个问题,但找不到与我遇到的问题类似的任何内容。非常感谢任何帮助!
你的武器属性是函数声明,不是变量定义。您必须在构造函数中传入默认值。
class ListOfWeapons
{
public:
ListOfWeapons() :
worn_greatsword(common, sword, "Worn Greatsword", 1, 2)
{
//...constructor stuff
}
protected:
private:
//function decl
//Weapon worn_greatsword(common, sword, "Worn Greatsword", 1, 2);
Weapon worn_greatsword;
};