【发布时间】:2016-02-07 19:52:56
【问题描述】:
我正在使用 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 风格转换或函数风格转换
【问题讨论】:
-
请read about how to ask good questions。您还应该学习如何创建Minimal, Complete, and Verifiable Example。您使用的是 Windows
localtime_s函数,还是 C 标准库中的localtime_s?这两个是不同的。请详细说明您遇到的问题,请向我们展示您在构建时可能收到的错误消息。 详细说明! -
所以这是您使用的Windows Visual Studio extension
localtime_s。阅读参考资料,检查并阅读函数返回的内容,