C++ C++;:从外部在类内声明枚举数,以便可以在私有成员中使用
不能在类之外声明嵌套枚举。在使用它之前,还必须定义它C++ C++;:从外部在类内声明枚举数,以便可以在私有成员中使用,c++,enums,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
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 );
};
作为旁注,C++11的
enum类
只需在类内声明-它可以在类外定义。不能在类外声明嵌套的枚举。在使用它之前,还必须定义它
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 );
};
作为旁注,C++11的
enum类
只需在类内声明-它可以在类外定义。看起来您正在尝试向前声明枚举。在C++03中,这是非法的。在C++11中,只要指定枚举的基础类型,就可以执行此操作。来自维基百科:
因此,如果编译器支持前向声明的枚举,则可以启用C++0x/C++11支持,将代码更改为:
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.
如果不是,则无法将枚举范围限定到类。您可以创建一个单独的命名空间,并使用typedef获得类似的语法:
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 );
};
然后在Printer类之外,在看到Printer类定义之前,需要将状态称为Printer::State,而不是Printer::State。在查看了Printer类定义之后,可以像往常一样将状态引用为Printer::States(因为typedef)
或者,如果删除名称空间,您只需将它们称为状态。看起来您正试图向前声明枚举。在C++03中,这是非法的。在C++11中,只要指定枚举的基础类型,就可以执行此操作。来自维基百科: 因此,如果编译器支持前向声明的枚举,则可以启用C++0x/C++11支持,将代码更改为:
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.
如果不是,则无法将枚举范围限定到类。您可以创建一个单独的命名空间,并使用typedef获得类似的语法:
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 );
};
然后在Printer类之外,在看到Printer类定义之前,需要将状态称为Printer::State,而不是Printer::State。在查看了Printer类定义之后,可以像往常一样将状态引用为Printer::States(因为typedef)
或者,如果您删除名称空间,您只需将它们引用为状态。或者您可以在声明
枚举后创建一个私有部分,并将其放入其中……您是在询问如何/在何处定义状态
?还是在声明枚举后创建一个私有部分并将其放入其中……您是在询问如何/在何处要定义状态
?