在 class 中使用后的 C++ 枚举声明

C++ enum declaration after use in class

我试图用一个枚举创建一个 class,以及一个 returns 这个枚举的变量的函数。但是我运行陷入了一个问题:

这个有效:

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 is undefined"... 为什么?我认为在使用后声明一个 class 成员不是问题。

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

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