“int”的转换无效;到“time_t*”和另一个错误
我收到以下错误:
错误:从“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要这样使用
时间
?该函数声明如下:向其传递一个
int
而不是一个指向time_t
的指针肯定是一个错误,编译器会正确地抱怨。假设您需要的只是
srand
的种子,只需使用time(NULL)
即可。如果您确实需要在参数中设置该值,请将a
和b
变量设置为time_t
类型,并使用time(& a)
。有关时间
的更多信息,阅读本文。Why would you use
time
this way? The function is declared as so:Passing it an
int
instead of a pointer to atime_t
is definitively an error, and the compiler correctly complains.Assuming all you need is a seed for the
srand
, just usetime(NULL)
. If you do need that value set in the argument, makea
andb
variables of typetime_t
, and usetime(&a)
. For further information abouttime
, read this.我几乎确定您只想调用 srand(time(NULL)) 并只执行一次。它为伪随机数生成器设置种子,并且使用实际时间是最常见的情况(每次运行应用程序时都会不同,因此生成的数字的顺序是)。要获取实际时间,请调用
time(NULL)
;如果你想将时间值存储在特定的time_t变量中,你可以传递它的指针而不是NULL。此代码:
和此代码:
甚至此代码:
是等效的(出于 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 calltime(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:
and this code:
and even this code:
are equivalent (for srand purposes).