因此,我目前正在研究基于文本的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;
};

关于c++ - 将枚举传递到不同文件中的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52434522/

10-12 01:46