“unsigned int”类型的参数与“time_t”类型的参数不兼容; c++错误

发布于 2025-01-11 12:20:14 字数 464 浏览 0 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒灬来后我 2025-01-18 12:20:14

这是您正在调用的函数的声明:

std::time_t 时间( std::time_t* arg );

该参数是一个std::time_t*,它是一个指针类型。

srand(time(static_cast(0)));

当需要指针时,将 static_cast(0) 作为参数传递是没有意义的。在 C++11 之前,这会起作用,并且参数会被解释为空指针,尽管它非常令人困惑。

从 C++11 开始,就不再允许这样做了。您可以像这样实现相同的目的:

std::srand(std::time(nullptr));

我正在学习一个简单的 C++ 教程

您遵循的教程显然已经过时了(大约十年),而且即使在编写时它的质量也不是很高。

This is the declaration of the function that you are calling:

std::time_t time( std::time_t* arg );

The parameter is a std::time_t* which is a pointer type.

srand(time(static_cast<unsigned>(0)));

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:

std::srand(std::time(nullptr));

Im following a simple c++ tutorial

The tutorial that you follow is apparently outdated (by about a decade), and it wasn't of high quality even when it was written.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文