Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

NiemandIstDa

macrumors newbie
Original poster
Aug 8, 2014
6
0
Hello,
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.

Code:
enum EElementsSymbol {
    H = 1,
    He,
    Li,
    Be,
    B,
    C,       // Carbon, ERROR: Redefinition of enumerator 'C'
    N,
    O,
    F,
    Ne,
    Na,
    Mg,
    Al,
    Si,
    // More symbol names
};

Anybody an idea why this is an error? And anyone an idea how to "fix" this?
 
Update

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:
Code:
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.
 
Update 2: News to thee problem

What I also found out is:

in the math project is a file named:
constantMath.h
There is a enum called
Code:
// 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:

Why??
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
 
Last edited:
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....
 

Attachments

  • Bildschirmfoto 2014-08-09 um 10.40.17.png
    Bildschirmfoto 2014-08-09 um 10.40.17.png
    381.6 KB · Views: 366
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.
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.:
Code:
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.
 
I found the error but I didn't understand why it comes up.

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

Code:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.