我收到了以下错误:
错误:无效转换为'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));
答案 0 :(得分:3)
为什么要以这种方式使用time
?该函数声明如下:
time_t time ( time_t * timer )
传递int
而不是指向time_t
的指针肯定是错误,编译器正确地抱怨。
假设您需要的只是srand
的种子,请使用time(NULL)
。如果确实需要在参数中设置该值,请创建a
类型的b
和time_t
变量,并使用time(&a)
。有关time
,read this。
答案 1 :(得分:2)
我几乎肯定你想打电话给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目的)。