我正在使用C++中的函数来获取月份的整数。我做了一些搜索,发现一个使用本地时间,但我不想设置它来删除警告,所以我需要使用localtime_s
。但是当我使用它时,我的指针不再起作用,我需要有人帮助我找到我缺少的指针。如何在C++中使用localtime_s指针
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <Windows.h>
#include "FolderTask.h"
#include <ctime> //used for getMonth
#include <string>
#include <fstream>
int getMonth()
{
struct tm newtime;
time_t now = time(0);
tm *ltm = localtime_s(&newtime,&now);
int Month = 1 + ltm->tm_mon;
return Month;
}
我得到的错误是:
错误C2440: '初始化':无法从 'errno_t' 转换为 'TM *' 注:从整型转换为指针类型要求 的reinterpret_cast,C样式转换或函数样式转换
请[阅读有关如何提出好的问题(http://stackoverflow.com/help/how-to-ask)。您还应该学习如何创建[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。你是否使用Windows ['localtime_s'](https://msdn.microsoft.com/en-us/library/a442x3ye.aspx)函数或['localtime_s'](http://en.cppreference。 com/w/c/chrono/localtime)从C标准库?这两个是不同的。请详细说明您遇到的问题,请向我们展示您在构建时可能遇到的错误消息。 *精心制作!* –
因此,您使用的是[Windows Visual Studio扩展'localtime_s'](https://msdn.microsoft.com/en-us/library/a442x3ye.aspx)。阅读参考资料,检查并阅读函数返回的内容, –