C++:从外部声明类内的枚举器,以便可以在私有成员中使用

发布于 2024-12-14 04:21:26 字数 725 浏览 1 评论 0原文

class Printer;
enum Printer::States;
class Printer {                  // choose one of monitor or cormonitor
    States taskStates[];
  public:
    enum States { Starting = 'S', Blocked = 'B', Unblocked = 'U', Finished = 'F', // general
          Napping = 'N', Awake = 'A',             // Santa
          Working = 'W', NeedHelp = 'H',          // elf
          OnVacation = 'V', CheckingIn = 'I',     // reindeer
          DeliveringToys = 'D', DoneDelivering = 'd', // Santa, reindeer
          Consulting = 'C', DoneConsulting = 'c' // Santa, elves
    };
    Printer();
    void print( unsigned int id, States state );
    void print( unsigned int id, States state, unsigned int numBlocked );
};
class Printer;
enum Printer::States;
class Printer {                  // choose one of monitor or cormonitor
    States taskStates[];
  public:
    enum States { Starting = 'S', Blocked = 'B', Unblocked = 'U', Finished = 'F', // general
          Napping = 'N', Awake = 'A',             // Santa
          Working = 'W', NeedHelp = 'H',          // elf
          OnVacation = 'V', CheckingIn = 'I',     // reindeer
          DeliveringToys = 'D', DoneDelivering = 'd', // Santa, reindeer
          Consulting = 'C', DoneConsulting = 'c' // Santa, elves
    };
    Printer();
    void print( unsigned int id, States state );
    void print( unsigned int id, States state, unsigned int numBlocked );
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔书几行 2024-12-21 04:21:26

您不能在类之外声明嵌套枚举。您还必须在使用它之前对其进行定义。

class Printer {                  // choose one of monitor or cormonitor
  public:
    enum States { Starting = 'S', Blocked = 'B', Unblocked = 'U', Finished = 'F', // general
      Napping = 'N', Awake = 'A',             // Santa
      Working = 'W', NeedHelp = 'H',          // elf
      OnVacation = 'V', CheckingIn = 'I',     // reindeer
      DeliveringToys = 'D', DoneDelivering = 'd', // Santa, reindeer
      Consulting = 'C', DoneConsulting = 'c' // Santa, elves
    };
  private:
    States taskStates[];
  public:
    Printer();
    void print( unsigned int id, States state );
    void print( unsigned int id, States state, unsigned int numBlocked );
};

顺便说一句,C++11 的 enum class 只需在类内部声明 - 它可以在类外部定义。

You can't declare nested enums outside of the class. You also have to have it be defined before you use it.

class Printer {                  // choose one of monitor or cormonitor
  public:
    enum States { Starting = 'S', Blocked = 'B', Unblocked = 'U', Finished = 'F', // general
      Napping = 'N', Awake = 'A',             // Santa
      Working = 'W', NeedHelp = 'H',          // elf
      OnVacation = 'V', CheckingIn = 'I',     // reindeer
      DeliveringToys = 'D', DoneDelivering = 'd', // Santa, reindeer
      Consulting = 'C', DoneConsulting = 'c' // Santa, elves
    };
  private:
    States taskStates[];
  public:
    Printer();
    void print( unsigned int id, States state );
    void print( unsigned int id, States state, unsigned int numBlocked );
};

As a side-note, C++11's enum class only has to be declared inside the class - it can be defined outside of it.

玻璃人 2024-12-21 04:21:26

看起来您正在尝试前向声明一个枚举。在 C++03 中这是非法的。在 C++11 中,只要指定枚举的基础类型就可以做到这一点。来自维基百科: http://en.wikipedia.org/wiki/C%2B% 2B11#Strongly_typed_enumerations

enum Enum1;                      // Illegal in C++03 and C++11; the underlying type cannot be determined.
enum Enum2 : unsigned int;       // Legal in C++11, the underlying type is explicitly specified.
enum class Enum3;                // Legal in C++11, the underlying type is int.
enum class Enum4 : unsigned int; // Legal C++11.
enum Enum2 : unsigned short;     // Illegal in C++11, because Enum2 was previously declared with a different underlying type.

因此,如果您的编译器支持前向声明枚举,您可以打开 C++0x/C++11 支持更改您的代码:

class Printer;
enum Printer::States : char;
class Printer {                  // choose one of monitor or cormonitor
    States taskStates[];
  public:
    enum States : char { Starting = 'S', Blocked = 'B', Unblocked = 'U', Finished = 'F', // general
          Napping = 'N', Awake = 'A',             // Santa
          Working = 'W', NeedHelp = 'H',          // elf
          OnVacation = 'V', CheckingIn = 'I',     // reindeer
          DeliveringToys = 'D', DoneDelivering = 'd', // Santa, reindeer
          Consulting = 'C', DoneConsulting = 'c' // Santa, elves
    };
    Printer();
    void print( unsigned int id, States state );
    void print( unsigned int id, States state, unsigned int numBlocked );
};

如果不是,您就无法将枚举范围限定为该类。您可以创建一个单独的命名空间并使用 typedef 来获得类似的语法:

class Printer;

namespace printer {
    enum States : char { Starting = 'S', Blocked = 'B', Unblocked = 'U', Finished = 'F', // general
          Napping = 'N', Awake = 'A',             // Santa
          Working = 'W', NeedHelp = 'H',          // elf
          OnVacation = 'V', CheckingIn = 'I',     // reindeer
          DeliveringToys = 'D', DoneDelivering = 'd', // Santa, reindeer
          Consulting = 'C', DoneConsulting = 'c' // Santa, elves
    };
}

class Printer {                  // choose one of monitor or cormonitor
    States taskStates[];
  public:
    typedef printer::States States;

    Printer();
    void print( unsigned int id, States state );
    void print( unsigned int id, States state, unsigned int numBlocked );
};

然后在 Printer 类之外,在看到 Printer 类定义之前,您需要将 States 称为 Printer::States 而不是 Printer::州。了解 Printer 类定义后,您可以像往常一样将状态称为 Printer::States(因为 typedef)。

或者,如果您删除名称空间,您只需将它们称为状态。

It looks like you are trying to forward-declare an enumeration. In C++03 this is illegal. In C++11, you can do this as long as you specify the underlying type of the enum. From wikipedia: http://en.wikipedia.org/wiki/C%2B%2B11#Strongly_typed_enumerations

enum Enum1;                      // Illegal in C++03 and C++11; the underlying type cannot be determined.
enum Enum2 : unsigned int;       // Legal in C++11, the underlying type is explicitly specified.
enum class Enum3;                // Legal in C++11, the underlying type is int.
enum class Enum4 : unsigned int; // Legal C++11.
enum Enum2 : unsigned short;     // Illegal in C++11, because Enum2 was previously declared with a different underlying type.

So if your compiler supports forward-declared enums, you can turn on C++0x/C++11 support change your code to:

class Printer;
enum Printer::States : char;
class Printer {                  // choose one of monitor or cormonitor
    States taskStates[];
  public:
    enum States : char { Starting = 'S', Blocked = 'B', Unblocked = 'U', Finished = 'F', // general
          Napping = 'N', Awake = 'A',             // Santa
          Working = 'W', NeedHelp = 'H',          // elf
          OnVacation = 'V', CheckingIn = 'I',     // reindeer
          DeliveringToys = 'D', DoneDelivering = 'd', // Santa, reindeer
          Consulting = 'C', DoneConsulting = 'c' // Santa, elves
    };
    Printer();
    void print( unsigned int id, States state );
    void print( unsigned int id, States state, unsigned int numBlocked );
};

If not, you can't scope your enum to the class. You could make a separate namespace and use a typedef to get similar syntax:

class Printer;

namespace printer {
    enum States : char { Starting = 'S', Blocked = 'B', Unblocked = 'U', Finished = 'F', // general
          Napping = 'N', Awake = 'A',             // Santa
          Working = 'W', NeedHelp = 'H',          // elf
          OnVacation = 'V', CheckingIn = 'I',     // reindeer
          DeliveringToys = 'D', DoneDelivering = 'd', // Santa, reindeer
          Consulting = 'C', DoneConsulting = 'c' // Santa, elves
    };
}

class Printer {                  // choose one of monitor or cormonitor
    States taskStates[];
  public:
    typedef printer::States States;

    Printer();
    void print( unsigned int id, States state );
    void print( unsigned int id, States state, unsigned int numBlocked );
};

And then outside of the Printer class, before the Printer class definiton has been seen, you'll need to refer to States as printer::States rather than Printer::states. After the Printer class definition has been seen, you can refer to States as Printer::States as usual (because of the typedef).

Alternatively, if you drop the namespace you just refer to them as States.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文