How to fix C++ error: no member named 'put_time' in namespace 'std'

Problem:

You are trying to use the std::put_time function in your C++ application, but you get the following compiler error message:

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;
          ~~~~~^

Solution

The std::put_time function is part of the <iomanip> header in C++, so you need to include this header in your code to use this function. Add the following code at the top of the file where the error occurs:

#include <iomanip>

Full example:

#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;
}