gpt4 book ai didi

c++ - 编译用于创建 boost.log 格式的代码时出错

转载 作者:行者123 更新时间:2023-11-28 04:26:03 24 4
gpt4 key购买 nike

我正在尝试使用选项 -std=c++14 在 Ubuntu 18.04 LTS 上使用 gcc 7.3.0 编译以下代码:

auto createLogFormatter()
{
boost::log::expressions::stream_type stream;
stream << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S ")
<< boost::log::expressions::attr<int>("Severity")
<< " " // ERROR HERE
<< boost::log::expressions::attr<boost::log::attributes::current_thread_id::value_type>("ThreadID")
<< " "
<< boost::log::expressions::smessage;
return stream;
}

出现奇怪的错误:

Log.cpp:51:13: error: no match for ‘operator<<’ (operand types are ‘boost::log::v2_mt_posix::expressions::aux::make_output_actor<boost::phoenix::actor<boost::log::v2_mt_posix::expressions::aux::attribute_output_terminal<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::phoenix::argument<2> >, 0> >, boost::posix_time::ptime, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::aux::light_function<void(boost::log::v2_mt_posix::basic_formatting_ostream<char>&, const boost::posix_time::ptime&)> > >, boost::log::v2_mt_posix::expressions::attribute_actor<int, boost::log::v2_mt_posix::fallback_to_none, void, boost::phoenix::actor>, int, false>::type {aka boost::phoenix::actor<boost::log::v2_mt_posix::expressions::aux::attribute_output_terminal<boost::phoenix::actor<boost::log::v2_mt_posix::expressions::aux::attribute_output_terminal<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::phoenix::argument<2> >, 0> >, boost::posix_time::ptime, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::aux::light_function<void(boost::log::v2_mt_posix::basic_formatting_ostream<char>&, const boost::posix_time::ptime&)> > >, int, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::to_log_fun<void> > >}’ and ‘const char [2]’)
stream << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S ")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<< boost::log::expressions::attr<int>("Severity")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<< " "
^~~~~~

根据 Boost 示例 here ,这样的事情应该有效:

    expr::stream
<< expr::attr< unsigned int >("LineID")
<< ": <" << logging::trivial::severity
<< "> " << expr::smessage

但是我收到这样的错误。我做错了什么?

最佳答案

编译器错误可能是由缺少包含引起的。 Lambda 表达式运算符由 Boost.Phoenix 在 boost/phoenix/operator.hpp 中提供。或者您可以只包含 boost/log/expressions.hpp,它提供所有 Boost.Log 表达式节点和运算符。

但除此之外,您的 createLogFormatter 函数不正确,因为它丢弃了整个格式化程序表达式。您不应该创建和返回 stream 对象,它只是启动格式化程序表达式的终端。您需要返回作为 lambda 表达式的结果创建的函数对象。

auto createLogFormatter()
{
auto fmt = stream << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S ")
<< boost::log::expressions::attr<int>("Severity")
<< " " // ERROR HERE
<< boost::log::expressions::attr<boost::log::attributes::current_thread_id::value_type>("ThreadID")
<< " "
<< boost::log::expressions::smessage;
return fmt;
}

如果你想有一个稳定的返回类型,你可以使用boost::log::formatter来包装表达式。

关于c++ - 编译用于创建 boost.log 格式的代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54286607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com