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

时间:2011-11-05 22:09:42

标签: 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 );
};

2 个答案:

答案 0 :(得分:2)

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

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只需要在类中声明 - 它可以在它之外定义。

答案 1 :(得分:1)

看起来您正在尝试向前声明枚举。在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类定义后,您可以像往常一样将状态称为Printer :: States(因为typedef)。

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