在編寫這些代碼:未聲明的枚舉?
#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