我是 C 的新手。我正在嘗試將當前日期和時間存盤為字串變數。
在這個問題上,我找到了答案,并安裝了該date.h庫。
但是,當我嘗試使用提供的代碼時,遇到了錯誤:
命名空間“std”沒有成員“格式”
盡管#include <format>在腳本的頂部。
我怎樣才能解決這個問題?
如果有幫助,我在 Windows 10 上使用 Visual Studio 2022。
這是我的代碼:
#include <iostream>
#include <chrono>
#include <date.h>
#include <type_traits>
#include <format>
int main()
{
std::cout << "The current time is ";
auto start_time = std::format("{:%F %T}", std::chrono::system_clock::now());
static_assert(std::is_same_v<decltype(start_time), std::string>{});
std::cout << start_time << "\n";
}
uj5u.com熱心網友回復:
std::format在 C 20 標準中被添加到 C 中。除非你用 C 20 編譯,否則你不會有std::format.
uj5u.com熱心網友回復:
截至 2021 年 12 月,std::format和其他一些C 20工具僅/std:c latest在Visual Studio 2019和2022 中的模式下可用。
這是一個報價:
作為實施 C 20 的一部分,有一些后期發現需要通過標準委員會的缺陷報告 (DR) 流程對 ISO C 20 標準進行更改。這包括這些功能的現有實作(預 DR)可在 /std:c latest 開關下獲得。我們還在跟蹤 DR 并在 /std:c latest 下實施這些問題解決方案。我們的計劃是在完整的標準庫 DR 實施完成后,在 /std:c 20 開關下提供這些功能。
當 Microsoft 完成所有 DR 的實施后,std::format將在/std:c 20交換機下可用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372498.html
下一篇:關閉前鍵盤中斷向終端添加數字
