gpt4 book ai didi

c++ - 使用 boost::log 输出用户定义的结构

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

我正在尝试使用 boost::log 库输出 boost::asio::streambuf 对象的内容。我按以下方式定义了 operator<< 重载:

ostream& operator<<(ostream& ostr, const boost::asio::streambuf& buffer)
{
for (size_t i = 0; i < buffer.size(); ++i)
{
ostr << hex << (int) buffer_cast<const char*>(buffer.data())[i] << " ";
}
return ostr;
}

但是在尝试输出缓冲区的内容之后:

 BOOST_LOG_TRIVIAL(trace) << buffer;

我有以下错误:

In file included from /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/log/sources/record_ostream.hpp:31:0, from /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/log/trivial.hpp:23, from /home/bobeff/work/asio_netcomm_poc/server/src/server.cpp:14: /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/log/utility/formatting_ostream.hpp: In instantiation of 'typename boost::log::v2_mt_posix::aux::enable_if_formatting_ostream::type boost::log::v2_mt_posix::operator<<(StreamT&, T&) [with StreamT = boost::log::v2_mt_posix::basic_formatting_ostream; T = boost::asio::basic_streambuf<>; typename boost::log::v2_mt_posix::aux::enable_if_formatting_ostream::type = boost::log::v2_mt_posix::basic_formatting_ostream&]': /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/log/sources/record_ostream.hpp:212:51: required from 'typename boost::log::v2_mt_posix::aux::enable_if_record_ostream::type boost::log::v2_mt_posix::operator<<(StreamT&, T&) [with StreamT = boost::log::v2_mt_posix::basic_record_ostream; T = boost::asio::basic_streambuf<>; typename boost::log::v2_mt_posix::aux::enable_if_record_ostream::type = boost::log::v2_mt_posix::basic_record_ostream&]' /home/bobeff/work/asio_netcomm_poc/server/src/server.cpp:88:47:
required from here /home/bobeff/work/asio_netcomm_poc/third_party/lib/boost/boost/log/utility/formatting_ostream.hpp:840:19: error: cannot bind 'boost::log::v2_mt_posix::basic_formatting_ostream::ostream_type {aka std::basic_ostream}' lvalue to 'std::basic_ostream&&' strm.stream() << value; ^ In file included from /usr/include/c++/4.8/iostream:39:0, from /home/bobeff/work/asio_netcomm_poc/server/src/server.cpp:1: /usr/include/c++/4.8/ostream:602:5: error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits; _Tp = boost::asio::basic_streambuf<>]' operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x) ^

缓冲区内容的正确输出方式是什么?

最佳答案

你的 operator<<未被 ADL 找到。参见 this 的第一部分回答。

关于c++ - 使用 boost::log 输出用户定义的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37886163/

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