Reputation: 33
I was wondering if I could use different enumerations in the same file where one is already present?
For this project I have elements which act as a genre for a number of books. I want to create different story's for the books and create a 2nd enum for them.
enum element {
ICE, FIRE, EARTH, WIND, NONE
};
enum book stories {
FK, IK, EK, WK, NONE
};
could this start issues with my compiler?
Upvotes: 1
Views: 79
Reputation: 1
Leaving the syntax error caused by book stories
aside, you can make your enum
declarations unambigous by putting them into a scope:
struct Elements {
enum element {
ICE, FIRE, EARTH, WIND, NONE
};
};
struct BookStories {
enum book_stories {
FK, IK, EK, WK, NONE
};
};
Referring to Elements::NONE
and BookStories::NONE
would be unambigous then.
Another way to make enums scoped is to use enum class
(since c++11):
enum class element {
ICE, FIRE, EARTH, WIND, NONE
};
enum class book_stories {
FK, IK, EK, WK, NONE
};
Upvotes: 1
Reputation: 1901
This is most generally solved by doing this:
typedef enum element_e {
E_ELEMENT_ERROR = -1,
E_ELEMENT_NONE,
/* ... */
} element_t;
typedef enum bookstories_e {
E_BOOKSTORIES_ERROR = -1,
E_BOOKSTORIES_NONE,
/* ... */
} bookstories_t;
So you can maintain C / C++ interop in your header files. If you don't have any C modules, use enum classes like the other answers describe.
Upvotes: 0
Reputation: 1821
Yes. The enumeration NONE
is present in both element
and book stories
(btw, you cannot name your enums with a space inside).
Use enum class
es if you want to use NONE
in both enumerations.
enum class element
{
ICE, FIRE, EARTH, WIND, NONE
};
enum class bookstories
{
FK, IK, EK, WK, NONE
};
Then you use your enum like this:
auto elem = element::NONE;
auto story = bookstories::NONE;
Upvotes: 2