Xcode: Redifinition of enumerator C??

Discussion in 'Mac Programming' started by NiemandIstDa, Aug 9, 2014.

  1. NiemandIstDa macrumors newbie

    Aug 8, 2014
    since last time I got so good answers (thanks),
    I've got a next question that is very confusing for me:

    I've got a file called elementsTable.h, where I have two enums inside. One holds all elements of the periodic system of elements by their name. That functions as it should. The second enum should be the same, this time with the symbol name of the elements.

    enum EElementsSymbol {
        H = 1,
        C,       // Carbon, ERROR: Redefinition of enumerator 'C'
        // More symbol names
    Anybody an idea why this is an error? And anyone an idea how to "fix" this?
  2. NiemandIstDa thread starter macrumors newbie

    Aug 8, 2014

    I found the error but I didn't understand why it comes up.

    I have another enum in another file, not even the same project (but workspace) named ERomanNumbers, where C is defined as 100.

    So far as I know/understand enumeration names only "lives" inside their enumerations, so why double names causing so much error?

    C++11 says explicitly that the compiler understand:
    enum State {				enum Direction {
    	unknown,					unknown,
    	off,						left,
    	on						right
    };						};
    Prior to C++11 there was an ambiguity between EState::unknown and EDirection::unknown. But that had been resolved in C++11 and enumeration constants now live only in their specific namespace, which can be a global namespace and an enumeration itself.
  3. NiemandIstDa, Aug 9, 2014
    Last edited: Aug 9, 2014

    NiemandIstDa thread starter macrumors newbie

    Aug 8, 2014
    Update 2: News to thee problem

    What I also found out is:

    in the math project is a file named:
    There is a enum called
    // Name: enum ERomanNumbers
    // Desc: Declares Roman numbers
    enum ERomanNumbers {
        I = 1,
        V = 5,
        X = 10,
        L = 50,
        C = 100,
        D = 500,
        M = 1000
    The formula.h includes the constantMath.h...

    The physics project includes the formula.h for formulas.

    Xcode says that the enumerator C is included in three files (actually all files that in the moment include the formula.h
    This is the exact error that Xcode gives me
    /Volumes/Extern/Programming/DemonShard/Code/Milestone/trajectory/chemistry/elementsTable.h:153:5: Redefinition of enumerator 'C'
    /Volumes/Extern/Programming/DemonShard/Code/Milestone/trajectory/chemistry/periodicTable.cpp:1:1: In file included from /Volumes/Extern/Programming/DemonShard/Code/Milestone/trajectory/chemistry/periodicTable.cpp:1:
    /Volumes/Extern/Programming/DemonShard/Code/Milestone/trajectory/chemistry/periodicTable.cpp:19:10: In file included from /Volumes/Extern/Programming/DemonShard/Code/Milestone/trajectory/chemistry/periodicTable.cpp:19:
    /Volumes/Extern/Programming/DemonShard/Code/Milestone/trajectory/chemistry/periodicTable.h:24:10: In file included from /Volumes/Extern/Programming/DemonShard/Code/Milestone/trajectory/chemistry/periodicTable.h:24:

    What am I missing/not understanding?

    BTW: All header files have an #ifndef/#define/#endif Guard, so it cannot be the case that the compiler thinks the file is linked multiple times
  4. NiemandIstDa thread starter macrumors newbie

    Aug 8, 2014
    Another Info

    I set all values inside the workspace/projects that I found to using C++11/LLVM5.1 to make sure C++11 is used.

    I changed one value LMVM5.1 language c++
    from gnu++ to c++11... was an error from me

    However the errors are the same....

    Attached Files:

  5. ncl macrumors member

    Aug 16, 2008
    I don’t think that this is correct. As far as I know, enums in C++11 have the same behaviour as in C++03 and C (enum values live in the enclosing scope of the enumeration). Otherwise, it would break quite a lot of code.

    However, to fix the problem you're facing, C++11 introduced 'enum class' (or 'enum struct'). In enum class, the enum values live within the scope of the enumeration . e.g.:
    enum class ERomanNumbers {
        I = 1,
        V = 5,
        X = 10,
        L = 50,
        C = 100,
        D = 500,
        M = 1000
    And then, to access the C value, you use ERomanNumbers::C.
  6. gnasher729 macrumors P6


    Nov 25, 2005
    As you discovered, the C language doesn't allow any two enumeration values to have the same name. One obvious reason is that if you use the enumeration value C for example, and you include the wrong header file, then the wrong enumeration value would be used, leading to bugs that are near impossible to find. That's the reason why even in a single source files, I would really hate to use constant names like C, H, L, He etc. - there is just too little information. It's one of those things that seem clever when you do them but come back to bite you.

    I'd write

    enum ElementSymbols {
        kElementH = 1, 
        kElementHe = 2, 
    I'd explictely write down the values, because if you forget an element, or put two elements in the wrong order, you have again a problem that is impossible to find. You may be absolutely sure that you can write them all down without forgetting one. I'm not. I'm not even sure that _I_ would do it without making mistakes.

Share This Page