我正在使用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)。閱讀參考資料,檢查並閱讀函數返回的內容, –