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


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++ 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:: States。在看到Printer类定义之后,您可以像往常一样将States引用为Printer::States(因为有typepedef)。

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

最新更新