2013-03-17 605 views
14

每當我嘗試使用srand功能我得到這個警告隱式聲明函數'time'[​​-Wimplicit-function-declaration] |

"implicit declaration of function 'time' [-Wimplicit-function-declaration]|" 

Windows錯誤報告運行時編譯的文件出現,
我是一個新手,C++編程,我在一本教科書上發現了這個,但它對我不起作用。

srand (time()); 
    int x= (rand()%10) +1; 
    int y= (rand()%10) +1; 
    printf("\nx=%d,y=%d", x,y); 

我需要糾正這個問題?

回答

24

你需要確保你#include正確的頭,在這種情況下:

#include <stdlib.h> // rand(), srand() 
#include <time.h> // time() 

如有疑問,請檢查手冊頁:

$ man rand

$ man time

還有一個問題:time()需要一個參數,它可以是NULL,所以你要srand()調用應該是:

srand(time(NULL)); 
+0

謝謝回答..我HV包括stdlib.h中,但包括time.h中它給了我另一個錯誤 錯誤後:參數太少函數「時間」 – 2013-03-17 07:25:46

+0

我的代碼包括像代碼在線編譯器精品工程pad .. im在我的電腦上使用codeblocks ide – 2013-03-17 07:33:01

+1

現在回答更新以覆蓋time()的缺失參數。 – 2013-03-17 07:49:23

1

注意time()功能同時使用其返回值,並在其地址參數當前時間(自1970年以來以秒錶示)。