您的位置:程序门 -> c/c++ -> c++ 语言



这种 枚举是什么意思啊??????


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


这种 枚举是什么意思啊??????
发表于:2008-01-21 20:52:12 楼主
enum       week       :       char{       monday='m',       tuesday='t',       wednesday='w',thursday='t',       friday='f',       saturday='s',       sunday='s'}things;  
things=sunday;  

cout   <   <things;  


有错啊  

error       c2593:       'operator       <   <'       is       ambiguous


ivor       horton's  
beginning       visual       c++?       2005  

enumeration       constants       are       type       int       by       default,       but       you       can       also       choose       to       specify       the       type       explicitly       by  
adding       a       colon       and       the       type       name       for       the       constants       following       the       enumeration       type       name       in       the       declaration.  
you       can       specify       the       type       for       the       enumeration       constants       as       any       of       the       signed       or       unsigned       integer  
types:       short,       int,       long,       and       char,       or       as       type       bool.  
thus,       you       could       define       the       enumeration       representing       days       of       the       week       as:  
enum       week:       char{       monday,       tuesday,       wednesday,       thursday,       friday,       saturday,       sunday};  
here       the       enumeration       constants       will       be       of       type       char       with       the       first       constant       as       0.       however,       with       the       constants  
as       type       char       you       might       prefer       to       initialize       them       explicitly,       like       this:  
enum       week       :       char{       monday='m',       tuesday='t',       wednesday='w',  
thursday='t',       friday='f',       saturday='s',       sunday='s'};  
now       the       values       for       the       constants       reflect       a       little       more       what       they       represent,       although       they       do       not       distinguish  
between       thursday       and       tuesday       or       between       saturday       and       sunday.       there       is       no       problem       with  
having       duplicate       values       for       the       constants,       but       of       course,       all       the       names       must       be       unique.  
here's       an       example       of       an       enumeration       with       bool       constants:  
enum       state       :       bool       {       on       =       true,       off};  
because       on       has       the       initial       value       true,       off       will       be       false.       if       there       were       subsequent       enumerations       constants  
specified,       they       would       alternate       in       value       by       default
发表于:2008-01-21 21:14:201楼 得分:0
把things=sunday;      
放到函数里阿,不能全局阿

enum   xxxx   :   char

表示枚举的数据类型是char,默认是int
发表于:2008-01-21 21:17:202楼 得分:0
这不是iso   c++吧~
发表于:2008-01-21 21:23:103楼 得分:0
我是放到main函数中的哦
发表于:2008-01-21 21:25:554楼 得分:0
c/c++ code
cout<<char)things;
发表于:2008-01-21 21:30:225楼 得分:0
也可以加上
c/c++ code
ostream& operator<<(ostream& o, week w) { return o<<char)w; }
发表于:2008-01-22 09:06:456楼 得分:0
楼主啊,因为你使用了vc定义枚举的扩展语法,而cout库vc并没有同步扩展,
所以,在你还是编程新手的阶段,不要使用任何只一个编译器支持的特有扩展语法。
发表于:2008-01-22 10:05:057楼 得分:0
楼上正解
发表于:2008-01-22 10:20:078楼 得分:0
谢谢
发表于:2008-01-22 14:24:539楼 得分:0
enum   week:char

的目的是要声明一个长度为char的枚举类型。这不是c++的标准语法,而是vc的一个扩展语法。

至于为什么使用operator   < <会导致二义性,原因如下:
由于week类型没有指定的operator   < <,所以一定要发生类型的隐式转换。

按照c++标准的规定enum类型会被提升为int类型,而按照你的定义enum类型又会被映射为char类型。而operator   < <针对int和char都有重载,所以编译器无法判断两个重载函数的优先级,产生了二义性错误
发表于:2008-01-22 14:25:5610楼 得分:0
解决方案就是显示指定week被转换到的类型,具体写法看chiyer   的代码
发表于:2008-01-22 14:29:1111楼 得分:0
官方支持。。


快速检索

最新资讯
热门点击