【问题标题】:C++ enums and inline header functionsC++ 枚举和内联标头函数
【发布时间】:2013-06-11 17:24:51
【问题描述】:

在 C++ 中使用 enum 时,如何创建 getter 和 setter?

例子:

enum class FieldLayerStates
{
        kEmpty = 0, // new to the game.
        kActive = 1, // has fields already in the same.
        kAdd = 2 // field layer is ready to have a field added.
};

FieldLayerStates _fieldLayerState;

inline FieldLayerStates getFieldLayerState() { return _fieldLayerState; };

inline void setFieldLayerState(FieldLayerStates i) { _fieldLayerState = i; };

我在内联函数中遇到错误:

: Unknown type name 'FieldLayerStates'
: Cannot initialize return object of type 'int' with an lvalue of type 'FieldLayer::FieldLayerStates'

当我去使用它时:

 // check the status of the layer to see if it has fields in it already (loaded from CCUserDefaults
if (getFields().empty())
{
    // nothing, this must be our first time in.
    setFieldLayerStatus(kEmpty);
}

它说kEmpty 未声明。

有人可以帮我解惑吗?

【问题讨论】:

  • 从 cmets 我猜你想使用一个位字段来存储不同的状态,枚举并不适合。您完全错误地陈述了您的问题,这就是为什么您不明白人们对您说的话的原因!我推荐一个具有公共属性的简单类,然后可能有 getter/setter,该类将充当你想要的位字段,丢失枚举。
  • @Paul,枚举适用于位掩码,但 scoped 枚举不方便
  • @JonathanWakely 枚举对于存储状态有什么好处?这不是你能做的:UserState::IsOnline = 1;或 UserState::IsOnline = 0;如果 UserState 是一个枚举。
  • @Paul, UserState state = IsOnline; if(state == IsOnline) ... 那有什么问题? C++ 标准甚至说枚举可用于实现位掩码,请参见第 17 条。也许您忽略了枚举类型和枚举数之间的区别?
  • 天哪,我搞砸了,我很抱歉我造成的混乱。 @JonathanWakely 感谢您的澄清。

标签: c++ c++11 enums compiler-errors


【解决方案1】:

您使用的是enum class,您确定这是您想要的吗?

如果您停止这样做,您的代码将起作用。

否则请参阅FieldLayerStates::kEmpty(因为enum class 的枚举数必须由它们的类型名称限定)

我不知道您为什么会收到 Unknown type name 'FieldLayerStates' 错误,因为您没有显示足够的上下文来理解代码,我猜我会说您正在尝试在类之外定义函数并且您需要说FieldLayer::FieldLayerStates

请显示完整代码,以便我们有机会看到您真正编译的内容。

【讨论】:

  • 删除 class 仍会导致未知类型错误。在这种情况下我使用 Enum 是否错误?
  • 很难知道,因为你没有显示你正在编译的实际代码。
  • 抱歉,Paul,干净并重新构建了它。除了设置变量的 .cpp 之外,我确实发布了所有内容。现在一切正常。我去查了enum class,我现在看到了我的错误。
【解决方案2】:
enum class Foo

是一个新的 C++11 语言特性,意思是“强类型和范围”枚举。它与仅仅有很大不同

enum Foo

当您使用强类型枚举时,您必须使用它们所包含的范围来限定它们。

enum class Colors { Red, Green, Blue };
enum class Moods  { Happy, Bored, Blue };

没有“类”,这将无法编译,因为您已经定义了两次“蓝色”。使用“类”,您实际上已经定义了两个范围,它们具有自己的私有范围枚举,需要“::”运算符才能访问。

“类”也是强类型的,这意味着它们不会强制转换 - 例如转换为整数类型 - 无需您显式转换它们。

enum COLORS { RED, GREEN, BLUE };
enum class Colors { Red, Green Blue };

int i = RED; // Legal
Colors j = Colors::Red; // Legal
int i = Colors::Red; // Illegal: Colors::Red is not an integer, it's a Colors.
Colors j = Red; // Illegal: You didn't specify the scope.
Colors j = RED; // Illegal: RED is an integer, not a Colors.

for (int i = RED; i <= BLUE; ++i) { cout << i << endl; } // Legal
// But Colors is not a numeric type, you can't do math or increment on it,
// so the following is illegal:
for (auto j = Colors::Red; j <= Colors::Blue; ++j)

enum class Flags = { Herp = 1, Derp = 2 };
Flags combo = Flags::Herp;
combo |= Flags::Derp; // Illegal, no '|' operator for Flags, casting required.

【讨论】:

    【解决方案3】:

    我想你只是想要这个

    enum FieldLayerStates
    {
        kEmpty = 0, // new to the game.
        kActive = 1, // has fields already in the same.
        kAdd = 2 // field layer is ready to have a field added.
    };
    

    【讨论】:

    • 我没有说它不会编译或无效,但根据他的使用方式,我相信这就是他想要的。
    • @Jason,它叫做 C++11,enum class 是有效的,但是你用错了,如果你只是说enum 你不会得到kEmpty is undeclared 错误
    猜你喜欢
    • 2015-09-14
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多