c++ - Stringstream error: cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' -
in creating simple exception class extension (where can construct error messages more easily), i've isolated error down following simple code:
#include <sstream> #include <string> class mycout { public: std::stringstream ssout; // removing gets rid of error template <typename t> mycout& operator << (const t &x) { // formatting return *this; } }; class myerr : public mycout { public: using mycout::operator<<; }; int main(int argc, const char* argv[]) { throw myerr() << "errormsg" << 1; mycout() << "message formatted"; return 0; }
which, on compiling, produces error:
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\sstream(724): error c2248: 'std::basic_ios<_elem,_traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_elem,_traits>' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_elem,_traits>::basic_ios' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> diagnostic occurred in compiler generated function 'std::basic_stringstream<_elem,_traits,_alloc>::basic_stringstream(const std::basic_stringstream<_elem,_traits,_alloc> &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char>, 1> _alloc=std::allocator<char> 1> ]
(in actual fact it's more complex , extends stuff std::runtime_error
)
i have seen previous answers state problem arises not passing streams reference, can't see how i'm not.
commenting out std::stringstream ssout
fixes issue. why, , how fix underlying problem?
you're throwing exception value, indeed recommended practice. however, means exception gets copied part of throw
statement, must have accessible copy constructor. , because have non-copyable member (std::stringstream
), must provide own copy ctor.
Comments
Post a Comment