2015-09-04 91 views
-2

我正在創建一個程序來保存當天的天氣信息。運行代碼後,我得到了struct tm重新定義的錯誤。我使用的Visual C++ 2008的編譯器C++時間結構錯誤

在這裏,在代碼塊運行,這是我的代碼:

#include<stdio.h> 
#include<string.h> 
#include<time.h> 

struct tm //date template 
    { 
     int tm_mday //day of month 
     int tm_mon; //month of year 
     int tm_year; //year 
     //char date[11]; 
    }; 

    struct weather 
    { 
     struct tm *wdate1; 
     int high_temp; 
     int low_temp; 
     int max_wind_speed; 
     int preciption; 
     char note[80]; 
    }; 


int main() 
{ 
    time_t wdate; 
    struct weather info[3]; 

    ctime(&wdate); 

    printf("%s",wdate); 

    return 0; 
} 
+0

「運行代碼後」 - >「*編譯*代碼後」。編譯時錯誤和運行時錯誤之間的巨大差異。 – crashmstr

回答

0

那是因爲你正在重新定義一個已經在time.h中定義了同名「tm」的結構。如果你想重新定義此模板結構相同的名字,那麼請不要導入

#include <time.h> 
在結構TM

而且,你缺少的tm_mday後終止,並命令

printf("%s",wdate); 

將導致因爲wdate不是char *而出錯。你應該俱樂部的最後兩行爲

printf("%s",ctime(&wdate)); 

希望這有助於!

+0

稍作修改,我建議:'#include ' –

+0

@LeFlou:是的,我們必須包含std命名空間中提到的。 –

0

重新定義結構TM - 如錯誤信息說 - 是你在做什麼。 tm結構體在time.h中定義。你在第5-11行再次定義它。你應該刪除這個定義,或者如果你想爲自己的用途定義一個自定義結構,就可以調用它。