“int”的转换无效;到“time_t*”和另一个错误

发布于 2024-11-24 14:39:56 字数 4473 浏览 1 评论 0原文

我收到以下错误:

错误:从“int”到“time_t*”的转换无效

错误:初始化“time_t time(time_t*)”的参数 1

我的代码是:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>

using namespace std;

int main()
{
char Grid[5][5];
Grid[0][0] = 'P';
Grid[0][1] = '-';
Grid[0][2] = '-';
Grid[0][3] = '-';
Grid[0][4] = '-';
Grid[1][0] = '-';
Grid[1][1] = 'T';
Grid[1][2] = '-';
Grid[1][3] = '-';
Grid[1][4] = '-';
Grid[2][0] = '-';
Grid[2][1] = '-';
Grid[2][2] = '-';
Grid[2][3] = '-';
Grid[2][4] = '-';
Grid[3][0] = '-';
Grid[3][1] = '-';
Grid[3][2] = '-';
Grid[3][3] = 'T';
Grid[3][4] = '-';
Grid[4][0] = '-';
Grid[4][1] = '-';
Grid[4][2] = '-';
Grid[4][3] = '-';
Grid[4][4] = 'X';
cout<<"P = Player (you)"<<endl<<
      "T = Trap (ouch!)"<<endl<<
      "X = Treasure (X marks the spot)"<<endl<<
      "Tell P where to go by using the commands 'right','left','up','down'."<<endl<<endl;
string command;
int x = 0;
int y = 0;
int a;
int b;
srand(time(a));
srand(time(b));
int enemy1 = rand()%4;
do
{
    cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
    cout<<Grid[0][0]<<Grid[0][1]<<Grid[0][2]<<Grid[0][3]<<Grid[0][4]<<endl<<
      Grid[1][0]<<Grid[1][1]<<Grid[1][2]<<Grid[1][3]<<Grid[1][4]<<endl<<
      Grid[2][0]<<Grid[2][1]<<Grid[2][2]<<Grid[2][3]<<Grid[2][4]<<endl<<
      Grid[3][0]<<Grid[3][1]<<Grid[3][2]<<Grid[3][3]<<Grid[3][4]<<endl<<
      Grid[4][0]<<Grid[4][1]<<Grid[4][2]<<Grid[4][3]<<Grid[4][4]<<endl;
    cout<<"Your command: ";
    cin>>command;
    cout<<endl;
    if(command=="up")
    {
        Grid[(x-1)][y] = 'P';
        x--;
        Grid[(x+1)][y] = '-';
    }
    if(command=="down")
    {
        Grid[(x+1)][y] = 'P';
        x++;
        Grid[(x-1)][y] = '-';
    }
    if(command=="right")
    {
        Grid[x][(y+1)] = 'P';
        y++;
        Grid[x][(y-1)] = '-';
    }
    if(command=="left")
    {
        Grid[x][(y-1)] = 'P';
        y--;
        Grid[x][(y+1)] = '-';
    }
    if(x==4 && y==4)
    {
        if(command=="down")
        {
            Grid[4][4] = 'P';
            cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
            cout<<Grid[0][0]<<Grid[0][1]<<Grid[0][2]<<Grid[0][3]<<Grid[0][4]<<endl<<
            Grid[1][0]<<Grid[1][1]<<Grid[1][2]<<Grid[1][3]<<Grid[1][4]<<endl<<
            Grid[2][0]<<Grid[2][1]<<Grid[2][2]<<Grid[2][3]<<Grid[2][4]<<endl<<
            Grid[3][0]<<Grid[3][1]<<Grid[3][2]<<Grid[3][3]<<Grid[3][4]<<endl<<
            Grid[4][0]<<Grid[4][1]<<Grid[4][2]<<Grid[4][3]<<Grid[4][4]<<endl;
            cout<<"You win!"<<endl;
        }
        if(command=="right")
        {
            Grid[4][4] = 'P';
            cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
            cout<<Grid[0][0]<<Grid[0][1]<<Grid[0][2]<<Grid[0][3]<<Grid[0][4]<<endl<<
            Grid[1][0]<<Grid[1][1]<<Grid[1][2]<<Grid[1][3]<<Grid[1][4]<<endl<<
            Grid[2][0]<<Grid[2][1]<<Grid[2][2]<<Grid[2][3]<<Grid[2][4]<<endl<<
            Grid[3][0]<<Grid[3][1]<<Grid[3][2]<<Grid[3][3]<<Grid[3][4]<<endl<<
            Grid[4][0]<<Grid[4][1]<<Grid[4][2]<<Grid[4][3]<<Grid[4][4]<<endl;
            cout<<"You win!"<<endl;
        }
    }
}
while(Grid[4][4] != 'P');
return 0;
}

我收到这些错误的行是第 45 行(和第 46 行,但这是相同的错误) 。 第 45 行代码如下:

srand(time(a));

I got the following errors:

error: invalid conversion from 'int' to 'time_t*'

error: initializing argument 1 of 'time_t time(time_t*)'

My code is:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>

using namespace std;

int main()
{
char Grid[5][5];
Grid[0][0] = 'P';
Grid[0][1] = '-';
Grid[0][2] = '-';
Grid[0][3] = '-';
Grid[0][4] = '-';
Grid[1][0] = '-';
Grid[1][1] = 'T';
Grid[1][2] = '-';
Grid[1][3] = '-';
Grid[1][4] = '-';
Grid[2][0] = '-';
Grid[2][1] = '-';
Grid[2][2] = '-';
Grid[2][3] = '-';
Grid[2][4] = '-';
Grid[3][0] = '-';
Grid[3][1] = '-';
Grid[3][2] = '-';
Grid[3][3] = 'T';
Grid[3][4] = '-';
Grid[4][0] = '-';
Grid[4][1] = '-';
Grid[4][2] = '-';
Grid[4][3] = '-';
Grid[4][4] = 'X';
cout<<"P = Player (you)"<<endl<<
      "T = Trap (ouch!)"<<endl<<
      "X = Treasure (X marks the spot)"<<endl<<
      "Tell P where to go by using the commands 'right','left','up','down'."<<endl<<endl;
string command;
int x = 0;
int y = 0;
int a;
int b;
srand(time(a));
srand(time(b));
int enemy1 = rand()%4;
do
{
    cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
    cout<<Grid[0][0]<<Grid[0][1]<<Grid[0][2]<<Grid[0][3]<<Grid[0][4]<<endl<<
      Grid[1][0]<<Grid[1][1]<<Grid[1][2]<<Grid[1][3]<<Grid[1][4]<<endl<<
      Grid[2][0]<<Grid[2][1]<<Grid[2][2]<<Grid[2][3]<<Grid[2][4]<<endl<<
      Grid[3][0]<<Grid[3][1]<<Grid[3][2]<<Grid[3][3]<<Grid[3][4]<<endl<<
      Grid[4][0]<<Grid[4][1]<<Grid[4][2]<<Grid[4][3]<<Grid[4][4]<<endl;
    cout<<"Your command: ";
    cin>>command;
    cout<<endl;
    if(command=="up")
    {
        Grid[(x-1)][y] = 'P';
        x--;
        Grid[(x+1)][y] = '-';
    }
    if(command=="down")
    {
        Grid[(x+1)][y] = 'P';
        x++;
        Grid[(x-1)][y] = '-';
    }
    if(command=="right")
    {
        Grid[x][(y+1)] = 'P';
        y++;
        Grid[x][(y-1)] = '-';
    }
    if(command=="left")
    {
        Grid[x][(y-1)] = 'P';
        y--;
        Grid[x][(y+1)] = '-';
    }
    if(x==4 && y==4)
    {
        if(command=="down")
        {
            Grid[4][4] = 'P';
            cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
            cout<<Grid[0][0]<<Grid[0][1]<<Grid[0][2]<<Grid[0][3]<<Grid[0][4]<<endl<<
            Grid[1][0]<<Grid[1][1]<<Grid[1][2]<<Grid[1][3]<<Grid[1][4]<<endl<<
            Grid[2][0]<<Grid[2][1]<<Grid[2][2]<<Grid[2][3]<<Grid[2][4]<<endl<<
            Grid[3][0]<<Grid[3][1]<<Grid[3][2]<<Grid[3][3]<<Grid[3][4]<<endl<<
            Grid[4][0]<<Grid[4][1]<<Grid[4][2]<<Grid[4][3]<<Grid[4][4]<<endl;
            cout<<"You win!"<<endl;
        }
        if(command=="right")
        {
            Grid[4][4] = 'P';
            cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
            cout<<Grid[0][0]<<Grid[0][1]<<Grid[0][2]<<Grid[0][3]<<Grid[0][4]<<endl<<
            Grid[1][0]<<Grid[1][1]<<Grid[1][2]<<Grid[1][3]<<Grid[1][4]<<endl<<
            Grid[2][0]<<Grid[2][1]<<Grid[2][2]<<Grid[2][3]<<Grid[2][4]<<endl<<
            Grid[3][0]<<Grid[3][1]<<Grid[3][2]<<Grid[3][3]<<Grid[3][4]<<endl<<
            Grid[4][0]<<Grid[4][1]<<Grid[4][2]<<Grid[4][3]<<Grid[4][4]<<endl;
            cout<<"You win!"<<endl;
        }
    }
}
while(Grid[4][4] != 'P');
return 0;
}

The line I got these errors on was line 45 (and 46 but it's the same errors).
The following code is on line 45:

srand(time(a));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦幻的味道 2024-12-01 14:39:57

为什么要这样使用时间?该函数声明如下:

time_t time ( time_t * timer )

向其传递一个 int 而不是一个指向 time_t 的指针肯定是一个错误,编译器会正确地抱怨。

假设您需要的只是 srand 的种子,只需使用 time(NULL) 即可。如果您确实需要在参数中设置该值,请将 ab 变量设置为 time_t 类型,并使用 time(& a)。有关时间的更多信息,阅读本文

Why would you use time this way? The function is declared as so:

time_t time ( time_t * timer )

Passing it an int instead of a pointer to a time_t is definitively an error, and the compiler correctly complains.

Assuming all you need is a seed for the srand, just use time(NULL). If you do need that value set in the argument, make a and b variables of type time_t, and use time(&a). For further information about time, read this.

墨离汐 2024-12-01 14:39:57

我几乎确定您只想调用 srand(time(NULL)) 并只执行一次。它为伪随机数生成器设置种子,并且使用实际时间是最常见的情况(每次运行应用程序时都会不同,因此生成的数字的顺序是)。要获取实际时间,请调用 time(NULL);如果你想将时间值存储在特定的time_t变量中,你可以传递它的指针而不是NULL。

此代码:

srand(time(NULL));

和此代码:

time_t now;
time(&now);
srand(now);

甚至此代码:

time_t now;
srand(time(&now));

是等效的(出于 srand 目的)。

I'm almost sure you want to call srand(time(NULL)) and do it only once. It sets a seed for a pseudo-random number generator and using actual time is most common case (it's different every time you run your application, hence the sequence of generated numbers is). To get the actual time you call time(NULL); if you want the time value to be stored in the specific time_t variable, you can pass its pointer instead of NULL.

This code:

srand(time(NULL));

and this code:

time_t now;
time(&now);
srand(now);

and even this code:

time_t now;
srand(time(&now));

are equivalent (for srand purposes).

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