警告 C4244:“参数”:从“time_t”到“unsigned int”的转换,可能丢失数据——C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9246536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data -- C++
提问by Gal Appelbaum
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... now I cannot have any errors or even warnings on this code but for some reason this warning keeps popping and I have no clue how to fix it... "warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data"
我制作了一个简单的程序,允许用户选择多个骰子然后猜测结果......我之前发布了这段代码,但问题错误,所以它被删除了......现在我不能对此有任何错误甚至警告代码,但由于某种原因,此警告不断弹出,我不知道如何解决它... “警告 C4244:'参数':从'time_t' 到'unsigned int' 的转换,可能会丢失数据”
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
int choice, dice, random;
int main(){
string decision;
srand ( time(NULL) );
while(decision != "no" || decision != "No")
{
std::cout << "how many dice would you like to use? ";
std::cin >> dice;
std::cout << "guess what number was thrown: ";
std::cin >> choice;
for(int i=0; i<dice;i++){
random = rand() % 6 + 1;
}
if( choice == random){
std::cout << "Congratulations, you got it right! \n";
std::cout << "Want to try again?(Yes/No) ";
std::cin >> decision;
} else{
std::cout << "Sorry, the number was " << random << "... better luck next time \n" ;
std::cout << "Want to try again?(Yes/No) ";
std::cin >> decision;
}
}
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
This is what I am trying to figure out, why am I getting this warning: warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
这就是我想弄清楚的,为什么我会收到此警告:警告 C4244:'argument':从'time_t' 转换为'unsigned int',可能会丢失数据
回答by Mysticial
That's because on your system, time_t
is a larger integer type than unsigned int
.
那是因为在您的系统上,time_t
是比unsigned int
.
time()
returns atime_t
which is probably a 64-bit integer.srand()
wants anunsigned int
which is probably a 32-bit integer.
time()
返回time_t
可能是 64 位整数的 a。srand()
想要一个unsigned int
可能是 32 位整数的an 。
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.
在这种情况下,向下转换(和潜在的数据丢失)并不重要,因为您只是使用它来为 RNG 做种。
回答by Pubby
This line involves an implicit cast from time_t
which time
returns to unsigned int
which srand
takes:
这一行涉及一个隐式转换,time_t
从中time
返回到unsigned int
哪个srand
需要:
srand ( time(NULL) );
You can make it an explicit cast instead:
您可以改为显式转换:
srand ( static_cast<unsigned int>(time(NULL)) );
回答by Ignacio Vazquez-Abrams
time()
returns a time_t
, which can be 32 or 64 bits. srand()
takes an unsigned int
, which is 32 bits. To be fair, you probably won't care since it's only being used as a seed for randomization.
time()
返回 a time_t
,可以是 32 或 64 位。srand()
需要一个unsigned int
,它是 32 位。公平地说,您可能不会在意,因为它只是用作随机化的种子。
回答by user3481361
This line involves an implicit cast from time_t which time returns to unsigned int which srand takes:
此行涉及从 time_t 的隐式转换,该时间返回到 srand 采用的 unsigned int:
srand ( time(NULL) );
You can make it an explicit cast instead:
您可以改为显式转换:
srand ( static_cast<unsigned int>(time(NULL)) );