2010-04-08 121 views
0

GCC 4.1.2 C99重新聲明枚舉

,我有以下枚舉在這個文件ccsmd.h:

enum options_e 
{ 
    acm = 0, 
    anm, 
    smd, 
    LAST_ENTRY, 

    ENTRY_COUNT = LAST_ENTRY 
}; 

enum function_mode_e 
{ 
    play = 0, 
    record, 
    bridge, 
    LAST_ENTRY, 

    ENTRY_COUNT = LAST_ENTRY 
}; 

錯誤消息:

error: redeclaration of enumerator ‘LAST_ENTRY’ 
error: previous definition of ‘LAST_ENTRY’ was here 
error: redeclaration of enumerator ‘ENTRY_COUNT’ 
error: previous definition of ‘ENTRY_COUNT’ was here 

我有LAST_ENTRY使我可以使用它作爲數組的索引。所以我喜歡在所有枚舉中保持相同。

回答

4

枚舉值與定義的枚舉存在於相同的名稱空間中。也就是說,在關於LAST_ENTRY,它類似於(使用非常鬆散這裏)到:

enum options_e { /* ... */); 

// for the LAST_ENTRY value in options_e 
static const int LAST_ENTRY = /* whatever */; 

enum function_mode_e { /* ... */); 

// for the LAST_ENTRY value in function_mode_e 
static const int LAST_ENTRY = /* whatever */; 

正如你所看到的,你重新定義LAST_ENTRY,因此錯誤。最好用你的枚舉值作爲區分它們的前綴:

enum options_e 
{ 
    options_e_acm = 0, 
    options_e_anm, 
    options_e_smd, 
    options_e_LAST_ENTRY, 
    options_e_ENTRY_COUNT = options_e_LAST_ENTRY // note this is redundant 
}; 

enum function_mode_e 
{ 
    function_mode_e_play = 0, 
    function_mode_e_record, 
    function_mode_e_bridge, 
    function_mode_e_LAST_ENTRY, 

    function_mode_e_ENTRY_COUNT = function_mode_e_LAST_ENTRY 
}; 

雖然現在你失去了你以前想要的東西。 (你能澄清那是什麼嗎?)