2012-11-29 148 views
0

我想保護對用於多線程日誌記錄和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;

+0

哪條線實際上給出錯誤?這是對'add_stream'的調用嗎?如果是這樣,你能提供方法簽名嗎? –

+0

'pBackend-> add_stream(spFileStream);'造成麻煩。我正在尋找原型,並將其放在問題中。 –

回答

2

從錯誤信息presuably pBackend->add_stream,當你給它一個shared_ptrThreadSafeStream這是一個完全無關的預期shared_ptrostream(!)類型。您需要創建一個超載的add_stream方法,最有可能與ThreadSafeStream一起使用。

+1

我無法修改boostlog代碼以適應我的需要......但我顯然需要在我的類「ThreadSafeStream」中繼承'std :: basic_ostream' –

相關問題