[英]C++11: “put_time is not a member of std” on Modern g++
我在新的編譯器上遇到了舊問題。 嘗試使用std::chrono
打印當前時間時,出現以下錯誤:
src/main.cpp:51:30: error: ‘put_time’ is not a member of ‘std’
std::cout << std::put_time(std::localtime(&time), "%c") << "\n\n";
^~~
令人討厭的代碼片段沒什么特別的:
#include <chrono>
...
auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << std::put_time(std::localtime(&time), "%c") << "\n\n";
這看起來很像GCC 4.x系列中返回的錯誤,在各處都有引用:
這個理論的唯一問題是我的GCC是現代的:
$ g++ --version
g++ (GCC) 6.3.1 20161221 (Red Hat 6.3.1-1)
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我還檢查了我的應用程序是否鏈接到適當的庫:
$ ldd build/myapp
...
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007fde97e7b000)
libc.so.6 => /lib64/libc.so.6 (0x00007fde97595000)
最后,在我的編譯標志中沒有發生任何異常情況:
g++ -g -Wall -Werror -std=c++11 -Wno-sign-compare src/main.cpp -c -o obj/main.o
我認為可以檢查的所有內容都表明這應該起作用。 簡而言之,有什么用呢?
問題是您輸入了錯誤的標題。 std::put_time
在聲明<iomanip>
而不是<chrono>
。
同樣,它也不會抱怨其他任何時間操作,例如
std::localtime
和to_time_t
。
std::localtime
在<ctime>
聲明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.