warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data — C++

前端 未结 4 1904
栀梦
栀梦 2020-12-13 19:36

I made a simple program that allows the user to pick a number of dice then guess the outcome... I posted this code before but with the wrong question so it was deleted... no

4条回答
  •  天涯浪人
    2020-12-13 19:55

    That's because on your system, time_t is a larger integer type than unsigned int.

    • time() returns a time_t which is probably a 64-bit integer.
    • srand() wants an unsigned int which is probably a 32-bit integer.

    Hence you get the warning. You can silence it with a cast:

    srand ( (unsigned int)time(NULL) );
    

    In this case, the downcast (and potential data loss) doesn't matter since you're only using it to seed the RNG.

提交回复
热议问题