2014-04-12 127 views
1

在编写这些代码:未声明的枚举?

#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)

我做错了吗?

回答

3

在C中,您不使用语法EnumType.SpecificEnum访问单独列举的常量。你只是说SpecificEnum。例如:

printf("%d", TRUE); 

当你写

printf("%d", Boolean.TRUE); 

Ç认为你试图去structunion命名Boolean并访问TRUE领域,因此编译器错误。

希望这会有所帮助!

+0

谢谢,很清楚!有什么方法可以使用'EnumType.SpecificEnum'语法? – cdonts

+0

@cdonts据我所知,C不支持这一点。在C++中,可以使用'enum class'来完成这个操作,虽然语法是'EnumType :: SpecificEnum'(而C++是一种不同的语言。) – templatetypedef

+0

在c中,这是不可能的。 – vanste25

0

你写了Boolean.只需写TRUEFALSE没有这个前缀。

1

只需在没有布尔值的情况下写入TRUE。

1
#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; 
}