【发布时间】:2014-11-27 03:36:51
【问题描述】:
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 重载了运算符
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()
/opt/gcc.4.9.1/include/c++/4.9.1/ostream:602:5:注意:正在初始化 'std::basic_ostream<_chart _traits>& 的参数 1 std::operator&&, const _Tp&) [与_CharT = char; _Traits = std::char_traits; _Tp = 标准::向量]' 运算符&& __os, const _Tp& __x)
我不知道为什么 boost 日志不起作用。它使用相同的
【问题讨论】:
-
Boost 日志使用重载的输出运算符,但它可能在另一个类类型上重载,而不是
std::ostream,这就是您的代码不起作用的原因。 -
作为一个实验,能否尝试在std命名空间中定义这个
<<操作符,并确认它是否可以编译? -
也许你的特质类型不匹配?尝试将
operator<<定义为模板template<typename CharT, typename Traits> inline std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& p, //etc。 -
bobah 是正确的,如果您在命名空间中定义函数,那么一切正常。
namespace std { signature here; }std::ostream& std::operator<<() {}