重载运算符<< Boost Log

Hun*_*unk 4 c++ boost stl c++11

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;
Run Code Online (Sandbox Code Playgroud)

你好,

我重载了我的运算符<< for std :: vector.当我使用std :: cout它运行良好,但有了升压日志我得到以下错误:

boost/log/utility/formatting_ostream.hpp:710:19:错误:无法将'boost :: log :: v2_mt_posix :: basic_formatting_ostream :: ostream_type {aka std :: basic_ostream}'左值绑定到'std :: basic_ostream &&'strm. stream()<< value;

/opt/gcc.4.9.1/include/c++/4.9.1/ostream:602:5:注意:初始化'std :: basic_ostream <_CharT,_Traits>&std :: operator <<(std ::)的参数1 basic_ostream <_CharT,_Traits> &&,const _Tp&)[with _CharT = char; _Traits = std :: char_traits; _Tp = std :: vector]'operator <<(basic_ostream <_CharT,_Traits> && __os,const _Tp&__ x)

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

Nip*_*dar 5

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

你应该重载operator << for 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;
}
Run Code Online (Sandbox Code Playgroud)

  • 然而,你需要得到它在*同一个命名空间*作为数据类型,你是在_point_xy_提升几何形状的情况下超载的,例如:`名字空间boost {命名空间几何{命名空间模型{命名空间D2 {模板<typename的CT, typename CS> inline std :: ostream&operator <<(std :: ostream&os,point_xy <CT,CS> const&pt){os <<"[point_xy:"<< pt.x()<<","< <pt.y()<<"]"; 返回; }}}} (2认同)