Elronnd Elronnd - 1 month ago 6
C++ Question

Issue with initializer list for vector of an enum class

I have an enum, and a couple of vectors of some of the contents of the enum. I want to change the enum to an enum class, for type safety, but I'm getting errors. Consider the following snippet:

#include <vector>

enum Colour {
red,
green,
blue
};

int main() {
const std::vector<Colour> something { red, green };
return 0;
}


It works fine. However, if I change the enum to an enum class, I get errors such as
error: ‘green’ was not declared in this scope
. What can I do?

ee2 ee2
Answer

Use Color::green for enum classes.

Comments