ant2009 ant2009 - 3 months ago 14
C Question

redeclaration of enumerator

gcc 4.1.2 c99

I have the following enum's in this file 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 messages:

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


I have the
LAST_ENTRY
so that I can use that as the index of an array. So I like to keep it the same across all enums.

Answer

Enumeration values exist in the same namespace as the enumeration is defined. That is, in regards to LAST_ENTRY, it's similar (used very loosely here) to:

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 */;

As you can see, you're redefining LAST_ENTRY, hence the error. It's better to prefix your enum values with something to differentiate them:

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

Though now you lose whatever you were going for before. (Could you clarify what that was?)

Comments