在编写这些代码:未声明的枚举?
#include <stdio.h>
enum Boolean
{
TRUE,
FALSE
};
int main(int argc, char **argv)
{
printf("%d", Boolean.TRUE);
return 0;
}
我越来越:
error: 'Boolean' undeclared (first use in this function)
我做错了吗?
在编写这些代码:未声明的枚举?
#include <stdio.h>
enum Boolean
{
TRUE,
FALSE
};
int main(int argc, char **argv)
{
printf("%d", Boolean.TRUE);
return 0;
}
我越来越:
error: 'Boolean' undeclared (first use in this function)
我做错了吗?
在C中,您不使用语法EnumType.SpecificEnum
访问单独列举的常量。你只是说SpecificEnum
。例如:
printf("%d", TRUE);
当你写
printf("%d", Boolean.TRUE);
Ç认为你试图去struct
或union
命名Boolean
并访问TRUE
领域,因此编译器错误。
希望这会有所帮助!
你写了Boolean.
只需写TRUE
或FALSE
没有这个前缀。
只需在没有布尔值的情况下写入TRUE。
#include <stdio.h>
enum Boolean { FALSE, TRUE };
struct {
const enum Boolean TRUE;
const enum Boolean FALSE;
} Boolean = { TRUE, FALSE };
int main(){
printf("%d\n", Boolean.TRUE);
return 0;
}
谢谢,很清楚!有什么方法可以使用'EnumType.SpecificEnum'语法? – cdonts
@cdonts据我所知,C不支持这一点。在C++中,可以使用'enum class'来完成这个操作,虽然语法是'EnumType :: SpecificEnum'(而C++是一种不同的语言。) – templatetypedef
在c中,这是不可能的。 – vanste25