“unsigned int”类型的参数与“time_t”类型的参数不兼容; c++错误
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <time.h>
int main() {
srand(time(static_cast<unsigned>(0)));
std::cout << "Hello from game 4!" << "\n";
system("PAUSE"); // end of application
return 0;
}
我收到错误:
argument of type `"unsigned int" is incompatible with the parameter of type "time_t*"
我正在遵循简单的 C++ 教程,因为我是初学者。
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <time.h>
int main() {
srand(time(static_cast<unsigned>(0)));
std::cout << "Hello from game 4!" << "\n";
system("PAUSE"); // end of application
return 0;
}
I got the error:
argument of type `"unsigned int" is incompatible with the parameter of type "time_t*"
I'm following a simple C++ tutorial as I am a beginner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您正在调用的函数的声明:
该参数是一个
std::time_t*
,它是一个指针类型。当需要指针时,将
static_cast(0)
作为参数传递是没有意义的。在 C++11 之前,这会起作用,并且参数会被解释为空指针,尽管它非常令人困惑。从 C++11 开始,就不再允许这样做了。您可以像这样实现相同的目的:
您遵循的教程显然已经过时了(大约十年),而且即使在编写时它的质量也不是很高。
This is the declaration of the function that you are calling:
The parameter is a
std::time_t*
which is a pointer type.It doesn't make sense to pass
static_cast<unsigned>(0)
as an argument when a pointer is expected. Prior to C++11 that would have worked and the argument would have been interpreted as a null pointer, although it is highly confusing.Since C++11, that hasn't been allowed anymore. You can achieve the same like this:
The tutorial that you follow is apparently outdated (by about a decade), and it wasn't of high quality even when it was written.