c++时间结构错误
Error in C++ time structures
我正在创建一个程序来保存当天的天气信息。运行代码后,我得到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行中再次定义它。您应该删除这个定义,或者如果您想为自己定义一个自定义结构体,则可以使用不同的名称。
相关文章:
- 创建结构的数组时遇到分段错误
- 使用具有结构不完整类型错误的模板
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 导致Segmentation错误的结构中的矢量
- 编译器指出结构和 int 说明的错误 2059
- 当结构位于单独的头文件中时'unregistered datatype'在 QML 中出现Q_GADGET错误
- 在 C 函数中返回结构会导致分段错误
- C++ 结构错误"调用'erase'没有匹配函数
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 结构中的错误"Incomplete type is not allowed"
- 体系结构x86_64的未定义符号:链接器错误
- 在另一个结构中声明内部结构会导致错误:结构使用无效
- 如何编写一个错误结构,该结构可以包含不同的强键枚举作为错误代码
- 错误:结构的初始值设定项太多
- 带有结构和指针的程序错误(结构,C++)
- 错误:结构的正向声明
- 使用结构体作为参数:错误:结构体未定义
- 定义错误结构体中的枚举
- "Was not declared in this scope"错误结构定义。C++
- GCC中可能的错误:结构中的文字字符串类型