一个枚举类型的小问题
#include <stdio.h>
#include <stdlib.h>
void main()
{
enum color {red,yellow,blue,white,black};
enum color i,j,k,pri;
i=red;
i++;//这句出错
printf( "%d\n ",i);
编译错误提示:
error C2676: binary '++ ' : 'enum main::color ' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.
[解决办法]
在C++中,++后缀操作符的操作数应该是算术类型或指针,而枚举不属于算术类型。
原来的程序之所以能运行,可能是C的标准不一样(没看过),也可能是编译器的实现,毕竟在这一点上,标准并没有做强制性的要求
[解决办法]
不可以这样用,如果想用,可以不要把i定义为enum,然后再做类型转换