C++:从外部在类内声明枚举器,因此可以在私有(private)成员中使用

标签 c++ enums

<分区>

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 {                  // 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 只需在类内声明 - 它可以在类外定义。

关于C++:从外部在类内声明枚举器,因此可以在私有(private)成员中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8023650/

相关文章:

c++ - 包含所有内容,以 "using"分隔

java - 编译错误 "cannot find symbol - class ENUM_NAME"

Scala 模式匹配 Java 枚举类型

java - 对常量使用接口(interface)还是类?

java - Enum.valueOf 的一般使用

c++ - 当返回类型是一个类时,指向返回值的指针的名称是什么?

c++ - 在模板类中匹配 CRTP

c++ - Gtk::Window set_visible 导致空白窗口

c++ - 让 VS 编译器捕捉有符号/无符号的赋值?

c# - 组枚举成员?