gpt4 book ai didi

c++ - 调用 'localtime_s' 时日历功能崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 15:05:53 28 4
gpt4 key购买 nike

我正在尝试制作一个日历,所以我使用 time_t 和 localtime_s 来存储时间和日期信息,然后将其存储在 Calendar 类的各个成员中:

void Callendar::Initialize()
{
time_t now = time(0);
tm *localTime = null;
localtime_s(localTime, &now);

LocalSeconds = localTime->tm_sec;
LocalMinutes = localTime->tm_min;
LocalHours = localTime->tm_hour;
LocalDays = localTime->tm_mday;
LocalMonths = localTime->tm_mon;
LocalYears = localTime->tm_year;
DaysSinceSunday = localTime->tm_wday;
DaysSinceJanuaryFirst = localTime->tm_yday;
HoursDaylightSavings = localTime->tm_isdst;
}

所有编译都很好,但是在运行时我得到:

Debug Assertion Failed!

Program: C:\Users\MyPC\Desktop\Framework\Framework\Debug\Framework.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\loctim64.c Line: 69

Expression: ( ptm != NULL )

关闭断言失败消息后,我在这一行收到标准调试错误:

static __inline errno_t __CRTDECL localtime_s(struct tm * _Tm, const time_t * _Time)
{
return _localtime64_s(_Tm, _Time);
}

这基本上是在 Calendar:Initialize() 中调用 *localtime_s(localTime, &now);* 的结果我可以使用此功能的弃用版本吗?我知道还有其他函数可以获取本地时间,但我不知道哪个是“正确的”函数。其他人向我建议我不应该使用“本地时间”,但貌似 localtime_s 也不行。

最佳答案

请参阅documentation

参数

_tm

Pointer to the time structure to be filled in.

这意味着函数期望第一个参数是指向有效 tm 实例的非空指针

改变

tm *localTime = null;
localtime_s(localTime, &now);

tm localTime;
localtime_s(&localTime, &now);

关于c++ - 调用 'localtime_s' 时日历功能崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15061771/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com