c++ - 神秘的编译错误: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>'

标签 c++ boost compiler-errors boost-log

我想通过 booSTLog 库保护对我用于多线程日志记录的日志文件的访问。

我试过这个流类

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 的原型(prototype):void add_stream(shared_ptr< stream_type > const& strm);typedef std::basic_ostream< target_char_type > stream_type;

最佳答案

从错误消息推测 pBackend->add_stream 应该是一个 shared_ptr (!) to an ostream 而你给它一个shared_ptr 到一个完全不相关的类型的 ThreadSafeStream。您需要创建一个最有可能与 ThreadSafeStream 一起使用的重载 add_stream 方法。

关于c++ - 神秘的编译错误: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13630751/

相关文章:

c++ - C++ Linux 应用程序的内存可视化

c++ - 使用点运算符触发强制转换运算符

c++ - 在同一个函数/程序中使用 WebSocket++ 服务器和客户端

class - 定义没有setter ecma6的getter - react

java - 为什么我的程序不能在 javac 编译器中运行,但可以在 IDE 中运行

c++ - Eigen 中的点积乘以转置?

c++ - 对列表 C++ 执行递归

c++ - boost 日志 : File Rotation

c++ - 将 boost::variant 传递给(来自)dll 是否安全?

actionscript-3 - 在函数外部使用变量。错误-参数数目不正确。预期1