【问题标题】:C++ enum declaration after use in class在类中使用后的 C++ 枚举声明
【发布时间】:2020-07-19 16:29:37
【问题描述】:

我尝试创建一个带有枚举的类,以及一个返回该枚举变量的函数。但是我遇到了一个问题:

这行得通:

class SizeBoxClass {

public:
    enum BoxType{xBox, yBox};

    BoxType intersects() {
        return xBox;
    }

}SizeBox;

但这不是:

class SizeBoxClass {

public:
    BoxType intersects() {
        return xBox;
    }

    enum BoxType { xBox, yBox };

}SizeBox;

我在函数声明的行得到“BoxType 未定义”... 为什么?我认为在使用后声明一个类成员不是问题。

【问题讨论】:

标签: c++ class enums member


【解决方案1】:

enum BoxType { xBox, yBox };类型声明(也是定义),而不是成员变量或成员函数。

类型需要在第一次使用前声明。

【讨论】:

  • 类型需要在首次使用前声明。种类。 class SizeBoxClass { public: auto intersects() { return BoxType{}; } enum BoxType { xBox, yBox }; }SizeBox; 是合法的,并且在声明之前“使用”该类型
  • @NathanOliver 是的,有一个奇怪的角落,您可以同时声明该类型的成员变量。好点子。有一个标准报价说明为什么这个变体是合法的?
猜你喜欢
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多