首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

对于一个枚举的有关问题

2012-10-10 
对于一个枚举的问题enum {two}aatwoint main(){}为什么错误,但是如果把atwo放函数里面就能通过![解决

对于一个枚举的问题
enum {two}a;
a=two;
int main()
{

}
为什么错误,但是如果把a=two放函数里面就能通过!

[解决办法]
An enumerated type is a user-defined type consisting of a set of named constants called enumerators. By default, the first enumerator has a value of 0, and each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator. Enumerators needn’t have unique values. The name of each enumerator is treated as a constant and must be unique within the scope where the enum is defined. An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined.
-------------------------------
把a=two放函数里面----这是显式的指定值

C/C++ code
// Example of the enum keywordenum Days               // Declare enum type Days{   saturday,            // saturday = 0 by default   sunday = 0,          // sunday = 0 as well   monday,              // monday = 1   tuesday,             // tuesday = 2   wednesday,           // etc.   thursday,   friday} today;                // Variable today has type Daysint  tuesday;           // Error, redefinition of tuesdayenum Days yesterday;    // Legal in C and C++Days tomorrow;          // Legal in C++ onlyyesterday = monday;int i = tuesday;        // Legal; i = 2yesterday = 0;          // Error; no conversionyesterday = (Days)0;    // Legal, but results undefined 

热点排行