简体   繁体   English

一个神秘的编译错误:无法从&#39;const boost :: shared_ptr转换 <T> &#39;to&#39;const boost :: shared_ptr <T> “

[英]A mysterious compilation error: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'

I wanted to protect the access to a log file that I use for multithreaded logging with boostlog library. 我想保护对用于带有boostlog库的多线程日志记录的日志文件的访问。

I tried this stream class 我试过这个流类

class ThreadSafeStream
{
public:
    template <typename TInput>
    const ThreadSafeStream& operator<< (const TInput &tInput) const
    {
         // some thread safe file access    
         return *this;
    }
};

using it this way (text_sink is a boostlog object): 以这种方式使用它(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

and I get this mysterious error: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>' 我得到了这个神秘的错误: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'

the whole compile error: 整个编译错误:

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

I suspect that I am not well defining the operator<<()... but I don't find what is wrong. 我怀疑我没有很好地定义运算符<<()...但我没有发现什么是错的。

this is the prototype of addStream: void add_stream(shared_ptr< stream_type > const& strm); 这是addStream的原型: void add_stream(shared_ptr< stream_type > const& strm); with typedef std::basic_ostream< target_char_type > stream_type; with typedef std::basic_ostream< target_char_type > stream_type;

From the error message presuably pBackend->add_stream is expected a shared_ptr (!) to an ostream while you're giving it a shared_ptr to a ThreadSafeStream which is a totally unrelated type. 从错误消息中可以预期pBackend->add_stream是一个shared_ptr (!)到一个ostream而你给它一个shared_ptr到一个完全不相关的ThreadSafeStream类型。 You'll need to create an overloaded add_stream method that works with ThreadSafeStream s most likely. 您需要创建一个重载的add_stream方法,该方法最有可能与ThreadSafeStream一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM