Changes between Version 1 and Version 2 of Programming/Cpp/StandardLibraryGotchaIncludeHeadersExplicitly
- Timestamp:
- Feb 19, 2016, 4:09:42 PM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Programming/Cpp/StandardLibraryGotchaIncludeHeadersExplicitly
v1 v2 4 4 [[Image(htdocs:images/cpp/tcpppl.jpg, alt="tcpppl", align=center)]] 5 5 6 I was using the `boost::log` library component and when compiling my code, I ran into a rather odd error converting a derived shared pointer to a base class shared pointer, i.e. implicitly converting a **`shared_ptr<std::ofstream>`** to a **`const shared_ptr<std::ostream> & amp;`**. Here's the code snippet, with the line causing the compiler error highlighed. Note that code has been simplified for clarity.6 I was using the `boost::log` library component and when compiling my code, I ran into a rather odd error converting a derived shared pointer to a base class shared pointer, i.e. implicitly converting a **`shared_ptr<std::ofstream>`** to a **`const shared_ptr<std::ostream> &`**. Here's the code snippet, with the line causing the compiler error highlighed. Note that code has been simplified for clarity. 7 7 8 8 {{{#!cpp … … 12 12 sinks::text_ostream_backend> TextSink; 13 13 14 bool Initialize(const std::string &logFile)14 bool Initialize(const std::string &logFile) 15 15 { 16 bool didSucceed =false;16 bool didSucceed = false; 17 17 18 18 try 19 19 { 20 auto sink =boost::make_shared<TextSink>();21 auto lfstream22 = 20 auto sink = boost::make_shared<TextSink>(); 21 auto lfstream 22 = boost::make_shared<std::ofstream>(logFile); 23 23 sink->locked_backend()->add_stream(lfstream); // <-- COMPILER ERROR HERE 24 24 core::get()->add_sink(sink); 25 BOOST_LOG(lgr_gtl) <<"LogSystem initialized...";25 BOOST_LOG(lgr_gtl) << "LogSystem initialized..."; 26 26 core::get()->flush(); 27 27 28 didSucceed =true;28 didSucceed = true; 29 29 } 30 catch (const std::exception&ex)30 catch (const std::exception& ex) 31 31 { 32 32 std::cout 33 << 34 << 35 std::cout << "Details:" <<std::endl;36 std::cout << ex.what() <<std::endl;33 << "LogSystem::Initialize() failed." 34 << std::endl; 35 std::cout << "Details:" << std::endl; 36 std::cout << ex.what() << std::endl; 37 37 } 38 return 38 return didSucceed; 39 39 } 40 40 }}} … … 43 43 the comment `COMPILER ERROR HERE`: 44 44 45 {{{ #!cpp46 error C2664: 'void boost::log::v2s_mt_nt6::sinks::basic_text_ostream_backend<char>::add_stream(const boost::shared_ptr<std::basic_ostream<char,std::char_traits<char>>> & amp;)': cannot convert argument 1 from 'boost::shared_ptr<T>' to 'const boost::shared_ptr<std::basic_ostream<char,std::char_traits<char>>> &'45 {{{ 46 error C2664: 'void boost::log::v2s_mt_nt6::sinks::basic_text_ostream_backend<char>::add_stream(const boost::shared_ptr<std::basic_ostream<char,std::char_traits<char>>> &)': cannot convert argument 1 from 'boost::shared_ptr<T>' to 'const boost::shared_ptr<std::basic_ostream<char,std::char_traits<char>>> &' 47 47 with 48 48 [ … … 62 62 63 63 {{{#!cpp 64 typedef basic_ofstream<char, char_traits<char> >ofstream;64 typedef basic_ofstream<char, char_traits<char> > ofstream; 65 65 }}} 66 66 … … 77 77 78 78 {{{#!cpp 79 template<class _Elem, class _Traits> classbasic_ofstream80 : public basic_ostream<_Elem,_Traits>79 template<class _Elem, class _Traits> class basic_ofstream 80 : public basic_ostream<_Elem, _Traits> 81 81 { // output stream associated with a C stream 82 82 ...