我想保护对用于多线程日志记录和boostlog库的日志文件的访问。一个神秘的编译错误:无法从'const boost :: shared_ptr <T>'转换为'const boost :: shared_ptr <T>'
我想这个流类
class ThreadSafeStream
{
public:
template <typename TInput>
const ThreadSafeStream& operator<< (const TInput &tInput) const
{
// some thread safe file access
return *this;
}
};
用这种方式(text_sink是boostlog对象):
//...
m_spSink.reset(new text_sink);
text_sink::locked_backend_ptr pBackend = m_spSink->locked_backend();
const boost::shared_ptr<ThreadSafeStream>& spFileStream = boost::make_shared<ThreadSafeStream>();
pBackend->add_stream(spFileStream); // this causes the compilation error
,我得到这个神秘的错误:cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'
整个编译错误:
Log.cpp(79): error C2664: 'boost::log2_mt_nt5::sinks::basic_text_ostream_backend<CharT>::add_stream' : cannot convert parameter 1 from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T> &'
1> with
1> [
1> CharT=char
1> ]
1> and
1> [
1> T=ThreadSafeStream
1> ]
1> and
1> [
1> T=std::basic_ostream<char,std::char_traits<char>>
1> ]
1> Reason: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'
1> with
1> [
1> T=ThreadSafeStream
1> ]
1> and
1> [
1> T=std::basic_ostream<char,std::char_traits<char>>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
我怀疑我没有很好地定义运营商< <()...但我不觉得有什么问题。
这是addStream的原型:void add_stream(shared_ptr<stream_type> const& strm);
与typedef std::basic_ostream<target_char_type> stream_type;
哪条线实际上给出错误?这是对'add_stream'的调用吗?如果是这样,你能提供方法签名吗? –
'pBackend-> add_stream(spFileStream);'造成麻烦。我正在寻找原型,并将其放在问题中。 –