jeudi 8 juillet 2021

Narrowing conversion required while using a P-RNG

I am trying to get a random number between a minimum value and a maximum value, but i get a narrowing conversion error. I have checked the template parameters of uniform_int_distribution so using a short shouldnt be an issue.

Template parameters
IntType - The result type generated by the generator. The effect is undefined if this is not one of short, int, long, long long, unsigned short, unsigned int, unsigned long, or unsigned long long.

In my attempt to fix the issue i casted the values to a short which did not seem to fix it. When 2 integer values have been attributed to randX and randY the error disappeared.

My current code after reverting the changes that have been made to it:

Fruit Fruit::getRandomFruit(const short& maxPosX, const short& maxPosY)
{
    bool isEaten{0};
    std::uniform_int_distribution<short> randX{ 1, maxPosX-1 };
    std::uniform_int_distribution<short> randY{ 1, maxPosY-1 };

    COORD frPos{randX(mersenne), randY(mersenne)};

    return {isEaten,
            frPos
    };
}

Error message:

Element '2': conversion from 'int' to '_Ty' requires a narrowing conversion

From my understanding the compiler sees the values as being integers and it's attempting to convert them.




Aucun commentaire:

Enregistrer un commentaire