过载运算符 << 提升日志

overload operator << Boost Log

本文关键字:lt 日志 运算符      更新时间:2023-10-16
inline std::ostream& operator<<(std::ostream& p, const std::vector<unsigned int>& vector){
  p << "[ ";
  for(auto i:vector){
    p << " "<< i << " ,";
  }
  p<< "]";
  return p;
}
#define LOG_DEBUG_MESSAGE BOOST_LOG_SEV(my_logger::get(), debug)

std::vector<unsigned int> test {1, 2, 3};
LOG_DEBUG_MESSAGE << "test "<< test;
std::cout << test  << std::endl;

你好

我重载了我的运算符<<以获得 std::vector。当我使用 std::cout 时,它运行良好,但使用提升日志时,我收到以下错误:

提升/日志/实用程序/formatting_ostream.hpp:710:19:错误:无法绑定 'boost::log::v2_mt_posix::basic_formatting_ostream::ostream_type {aka std::basic_ostream}' lvalue to 'std::basic_ostream&&' strm.stream(( <<值;

/opt/gcc.4.9.1/

include/c++/4.9.1/ostream:602:5:注意:正在初始化 'std::basic_ostream<_CharT, _Traits>& 的参数 1 std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&( [_CharT = 字符;_Traits = std::char_traits; _Tp = std::vector]' operator<<(basic_ostream<_CharT, _Traits>&__os, const _Tp& __x(

我不知道为什么提升日志不起作用。它使用相同的<<运算符或?在其他具有自己的类的示例中,它适用于重载。我不知道我错过了什么。有人知道我如何解决此错误吗?

'boost::log::basic_formatting_ostream 不是从 std::ostream 派生的。

你应该重载 std::vector 的运算符<<,重载运算符应该将 boost::log::formatting_ostream& 作为它的第一个参数。

检查下面修改后的代码示例:

inline boost::log::formatting_ostream& operator<<(boost::log::formatting_ostream& p,  std::vector<int>& vector)
{
        p << "[ ";
        for(auto i:vector){
            p << " "<< i << " ,";
        }
        p<< "]";
        return p;
}