簡體   English   中英

使用localtime_s時訪問tm結構的成員

[英]Accessing the Members of a tm struct When Using localtime_s

我正在嘗試使用以下代碼以HH:MM:SS格式返回時間。 我以前使用的是localtime但是鑒於此已貶值,因此我localtime_s這樣切換到localtime_s

time_t t;
struct tm now;
localtime_s(&now, &t);
std::string stimeNow = std::to_string(now->tm_hour) + ":" + std::to_string(now->tm_min) +":" + std::to_string(now->tm_sec);

但是MSVC uderlines now- now->tm_hournow->tm_hour now->tm_minnow->tm_sec

Error: expression must have a pointer type

當我編譯它時,我收到以下錯誤:

error C2819: type 'tm' does not have an overloaded member 'operator ->'

我對指針和結構無能為力,所以有人可以告訴我我哪里出錯了以及如何修復它。

其用法示例如下:

/* localtime example */
  #include <stdio.h>      /* puts, printf */
  #include <time.h>       /* time_t, struct tm, time, localtime */

int main ()
{
    time_t rawtime;
    struct tm * timeinfo;
    time (&rawtime);
    timeinfo = localtime (&rawtime);
    printf ("Current local time and date: %s", asctime(timeinfo));
    return 0;
  }

順便說一句,您可以通過在源文件中添加以下代碼來禁用VS中不推薦使用的警告(錯誤):

#pragma warning (disable : <The error or warning code it gives when you use the unsafe version>)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM