Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same names in multiple enum [duplicate]

Tags:

c++

I have several enum that has the same field names:

enum Response
{
  Ok = 0, 
  Busy = 1
}

enum Status
{
  Ok = 0, 
  LoggedOut = 1
}

This gives the error:

error: redeclaration of 'Ok'

How do to solve this problem?

UPD

Trying to use enum class:

enum class Response
{
  Ok = 0, 
  Busy = 1
}
Status s1 = Status::Ok ;

Got error:

Error: 'Status' is not a class or namespace
     Status s1 = Status::Ok ;
                 ^
like image 479
vico Avatar asked Jul 23 '26 18:07

vico


1 Answers

I would switch to using enum class

enum class Response
{
  Ok = 0, 
  Busy = 1
};

enum class Status
{
  Ok = 0, 
  LoggedOut = 1
};

then you can refer to an enum value without ambiguity

Status s = Status::Ok;
like image 165
Cory Kramer Avatar answered Jul 26 '26 06:07

Cory Kramer