将枚举传递到不同文件中的对象中
Passing Enums Into Objects in Different Files
所以我目前正在开发一个基于文本的RPG,我遇到了一个奇怪的问题。在进行武器编码时,我选择使用枚举来表示武器的类型和稀有性。我已经为我的Weapon
课编写了所有内容;然而,当我尝试创建一个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;
};
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在h文件中包含.o对象文件
- 如何使用单独文件中的派生类访问友元函数对象
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 使用逗号解析文件对象?
- 使用 CIN COUT 显示垃圾值的文件对象?
- 如何确保我的"ifstream"文件对象指向的文件内容已更新?
- 如何从 GDI+ 图元文件对象获取原始图元文件字节
- pyQt5 将文件对象传递给 QWebEngineView
- 从文件对象的当前位置删除字符
- 使用相同的文件对象按顺序写入和读取文件
- 将 ETW 文件对象与 DiskIO 事件相关联
- 如何在构建时将 JSON 文件/对象包含在C++中
- 如何使用运算符<<通过 << 将文件对象馈送到我的类?
- 确定两个文件路径引用同一文件对象的最佳方法是什么
- 文件映射对象和文件对象可以互换使用吗
- 如何判断给定点是否在STereoLithography(.stl)文件对象c++内
- 如何使用Windows Portable Devices C++API获取MTP设备公开的文件夹中的所有文件(对象)的列
- 如何使用结构传递文件对象
- 文件对象->文件名不返回文件的完整路径