gpt4 book ai didi

c - 从 int 到 main()::colors 的转换无效?

转载 作者:行者123 更新时间:2023-11-30 16:01:25 26 4
gpt4 key购买 nike

我是 C 新手,我一直在阅读一本名为“C 编程的简单步骤”的书中的概念和示例代码。

因此,我逐个字符地输入此示例程序:

#include <stdio.h>
int main()
{
/* declare a sequence of constants */
enum colors
{ RED=1,YELLOW,GREEN,BROWN,BLUE,PINK,BLACK };

/* Declare a variable of the enumerated data type */

enum colors fingers;

/* assign valid constants from the colors list */
/* -----THIS IS THE ERROR LINE BELOW---------- */

fingers = (enum colors) PINK + BROWN;


/*-display the value in the variable */
printf("Value: %d\n", fingers);

return 0;
}

我收到此错误:

13 C:\Users\mjohearn\Documents\pet projects\constant types NOT WORKING\enumtypes.cpp invalid conversion from `int' to `main()::colors'

由于某种原因,编译器无法识别手指

如果有人能帮我解决这个问题,我将非常感激。

最佳答案

尝试

fingers = (enum colors) ( PINK + BROWN ) ;

我相信强制转换运算符比加法运算符绑定(bind)得更紧密。

关于c - 从 int 到 main()::colors 的转换无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6653464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com