mercredi 22 mai 2019

Is there any way to get random from enum class in c++?

I want to fill a variable with random element from enum class.

So I tried set enum class type to int and pass last enum from enum class to rand:

enum class Enumerator: int
{
    en1=0,
    en2,
    en3,
    ensCount
};

int main()
{
    srand(time(NULL));
    auto a=static_cast<Enumerator>(rand()%Enumerator::ensCount);
    return 0;
}

Result is "no match for «operator%» (operand types are «int» and «Enumerator»)" error.




Aucun commentaire:

Enregistrer un commentaire