如何修复 C++ 错误:no member named 'put_time' in namespace 'std'
问题:
你正在尝试在 C++ 应用程序中使用 std::put_time 函数,但收到以下编译器错误消息:
error.txt
main.cpp:17:16: error: no member named 'put_time' in namespace 'std'
ss << std::put_time(std::localtime(&localTime), "%F_%H-%M-%S") << extension;
~~~~~^解决方案
std::put_time 函数是 C++ <iomanip> 头文件的一部分,所以你需要在代码中包含此头文件才能使用此函数。在错误发生的文件顶部添加以下代码:
main.cpp
#include <iomanip>完整示例:
main.cpp
#include <iostream>
#include <iomanip>
#include <chrono>
int main() {
auto now = std::chrono::system_clock::now();
auto localTime = std::chrono::system_clock::to_time_t(now);
std::cout << std::put_time(std::localtime(&localTime), "%F %T") << std::endl;
return 0;
}Check out similar posts by category:
C/C++, GCC Errors
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow