为什么fmt :: format不接受字符串作为参数?

如何解决为什么fmt :: format不接受字符串作为参数?

我之前已经成功使用过fmt库,所以我不确定为什么会遇到这个问题。首先,我将向您展示产生错误代码

void Logger::AttachSink(const std::string & id,LogSink sink) {
    using namespace std::literals;
    auto id_found = sinks.find(id) != std::endl(sinks);
    if(!id_found) {
        sinks.insert(std::make_pair(id,sink));
        Info(
            fmt::format(
                "Sink \"{}\" Attached"s,id
            ) // This is the line where the error is apparently happening,and I'm assuming the `id` is the culprit for some reason.
        );
    }
}

这是构建日志(注意:我使用VSCode作为编辑器,并使用CMake使用Visual Studio 2019的编译器构建项目):

[main] Building folder: [WORKSPACE]
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.exe" --build [WORKSPACE]/build --config Debug --target ALL_BUILD -- /maxcpucount:6
[build] Microsoft (R) Build Engine version 16.7.0+b89cb5fde for .NET Framework
[build] copyright (C) Microsoft Corporation. All rights reserved.
[build] 
[build]   logging.cpp
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1073,9): error C2338: Cannot format argument. To make type T formattable provide a formatter<T> specialization: https://fmt.dev/latest/api.html#formatting-user-defined-types [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1070): message : while compiling class template member function 'int fmt::v7::detail::arg_mapper<Context>::map(...)' [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1259): message : see reference to function template instantiation 'int fmt::v7::detail::arg_mapper<Context>::map(...)' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1084): message : see reference to class template instantiation 'fmt::v7::detail::arg_mapper<Context>' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1241): message : see reference to alias template instantiation 'fmt::v7::detail::mapped_type_constant<const std::basic_string<char,std::char_traits<char>,std::allocator<char>>,Context>' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1400): message : see reference to function template instantiation 'unsigned __int64 fmt::v7::detail::encode_types<Context,const std::basic_string<char,>(void)' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               Context=fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>
[build]           ]
[build] [WORKSPACE]\build\vcpkg_installed\x64-windows\include\fmt\core.h(1834): message : see reference to class template instantiation 'fmt::v7::format_arg_store<fmt::v7::basic_format_context<std::back_insert_iterator<fmt::v7::detail::buffer<char>>,char>,std::allocator<char>>>' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build] [WORKSPACE]\drakeng\logging.cpp(24): message : see reference to function template instantiation 'std::basic_string<char,std::allocator<char>> fmt::v7::format<std::string,const std::string&,char>(const S &,const std::string &)' being compiled [[WORKSPACE]\build\drakeng\drakeng.vcxproj]
[build]           with
[build]           [
[build]               S=std::string
[build]           ]
[build] Build finished with exit code 1

错误使我听起来好像fmt没有专长的fmt::formatter<std::string>专长,所以我要么配置错误,要么不包含标题或其他内容。坦白说,我很困惑。一旦我有了答案,知道我的运气就很愚蠢了。无论如何,在此先感谢您提供的任何帮助。

解决方法

好吧,所以显然错误不是我所认为的引起的(谢谢,Visual C ++编译器错误消息,超级有用!/ s)。因此,请解释一下实际发生的情况:

  • Logger::Info调用了更通用的函数Logger::Log(LoggingLevel level,const std::string & message)
  • Logger::Log还在其中调用fmt::format来格式化一些其他信息以保存到日志中。
  • fmt::format调用的参数之一是level,然后需要将其转换为字符串(我选择使用ToString函数而不是使用{{ 1}}。
  • 问题是fmt::formatter函数返回ToString以“干净”地处理无效值。好吧,显然我忘记了在返回值上调用std::optional<std::string>,而导致错误的原因是那个

不幸的是,所有错误消息都没有提到与该行有关的内容,因此浪费了数分钟/数小时。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?