我在一个新编译器上遇到了一个老问题。在尝试使用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
我能想到的每一件事都表明这应该是可行的。那么,简而言之,是什么原因呢?
发布于 2017-07-13 02:51:29
问题是您包含了错误的标题。std::put_time
是在<iomanip>
而不是<chrono>
中声明的。
此外,它也没有抱怨任何其他的时间操作,如
std::localtime
和to_time_t
。
std::localtime
在<ctime>
中声明。
https://stackoverflow.com/questions/45070588
复制相似问题