c++时间结构错误

Error in C++ time structures

本文关键字:错误 结构 时间 c++      更新时间:2023-10-16

我正在创建一个程序来保存当天的天气信息。运行代码后,我得到struct tm重定义错误。我使用visual c++ 2008作为编译器在代码块中运行这个

下面是我的代码:
#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;
}

这是因为您正在重新定义一个已经在time.h中以相同名称"tm"定义的结构。如果你试图用相同的结构名重新定义这个模板,那么请不要导入

 #include <time.h>

同样在结构体tm中,在tm_mday和命令

之后缺少一个结束符。
 printf("%s",wdate);

将导致in错误,因为wdate不是char *。你应该把最后两行写成

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

希望这对你有帮助!

重新定义struct tm——就像错误信息所说的那样——正是您正在做的事情。tm结构体在time.h中定义。在第5-11行中再次定义它。您应该删除这个定义,或者如果您想为自己定义一个自定义结构体,则可以使用不同的名称。