作者khoguan (Khoguan Phuann)
看板C_and_CPP
标题Re: [问题] about enum
时间Sat May 21 14:08:03 2005
※ 引述《glob ( 人真复杂)》之铭言:
: 看书上写 enum 的用法
: enum spectrum {red,orange,green};
: char *colors[] = {"red","orange","green"};
: void main()
: {
: char c[10];
: enum spectrum color;
: ..
: ..
: ..
: ..
: if(gets[c]!=NULL)
^ ^
(c)
: {
: for(color=red;color<=green;color++) // 这行失败了!!!
: if(strcmp(c,colors[color])== 0)
: break;
: }
: .
: .
: .
: }
: error information
: error C2676: binary '++' :
: 'enum spectrum' does not define this operator or a conversion
: to a type acceptable to the predefined operator
: Error executing cl.exe.
: ps. 不大懂他再说什麽说 是要我们自己定义吗?!
你写的都是用 C 的语法,却使用 C++ compiler 来编译,
上述若是用 C 规则来编就可以通过。在 VC++ 中的设定是
「属性->C/C++->进阶->编译成 C 程式码」,可针对个别档案,
或整个专案来设定。
C++ 对於 enum type 是把它当做 user-defined type, 当 enum
variable 做一般的数学运算时,如
enum spectrum c1 = red, c2 = orange;
enum spectrum c3 = c1 + c2; /* 加的时候转成int,结果赋予 c3, C ok
但 C++不能直接将 int assign给enum,必需
做 explicit cast
*/
enum spectrum c4 = (enum spectrum)(c1 + c2); /* C/C++ ok */
而你所遇到的 ++ 这个 operator, C 可以用在 enum type,
而 C++ 则要求你要自己定义给你的 enum type 才能用。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.130.208.168
1F:→ glob:非常感谢您!!! 140.114.219.51 05/21