The problem is occurring because enum
in C++ does not define a namespace, but rather a constant that can be implicitly converted to an int
. Thus your code is similar to writing:
int Ok = 0;
int Busy = 1;
int Ok = 0;
int LoggedOut = 1;
Which, of course, produces a conflict. There are three possible approaches you can take to solve this, of which the first is the most desirable but the others are possible alternatives if you cannot use it for some reason:
(1) If you can use features from C++ 11 - which you should be able to - you can replace the enum
with the newer enum class
. Which both defines a separate namespace and is strongly typed, stopping you from mixing different enums together (see this question for more). e.g.
enum class Response
{
Ok = 0,
Busy = 1
};
enum class Status
{
Ok = 0,
LoggedOut = 1
};
You will now need to refer to this values in a scoped fashion, e.g. Status::Ok
or Response::Busy
.
(2) You can change the names to include the enum name:
enum Response
{
Response_Ok = 0,
Response_Busy = 1
};
(3) You can encase the enums themselves in a namespace:
namespace Status
{
enum Status
{
Ok = 0,
LoggedOut = 1
};
}