3

In creating a simple exception class extension (where I can construct error messages more easily), I've isolated an error down to the following simple code:

#include <sstream>
#include <string>
class myCout {
public:
    std::stringstream ssOut;    // Removing this gets rid of error
    template <typename T> myCout& operator << (const T &x) {
        // Do some formatting
        return *this;
    }
};

class myErr : public myCout {
public:
    using myCout::operator<<;
};

int main(int argc, const char* argv[]) {
    throw myErr() << "ErrorMsg" << 1;
    myCout() << "Message Will Be Formatted";
    return 0;
}

Which, on compiling, produces this 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>          with
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>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_stringstream<_Elem,_Traits,_Alloc>::basic_stringstream(const std::basic_stringstream<_Elem,_Traits,_Alloc> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Alloc=std::allocator<char>
1>          ]

(In actual fact it's more complex and extends stuff like std::runtime_error)

I have seen previous answers which state the problem arises from not passing streams by reference, but I can't see how I'm not.

Commenting out the std::stringstream ssOut fixes the issue. Why, and how do I fix the underlying problem?

Community
  • 1
  • 1
mchen
  • 9,808
  • 17
  • 72
  • 125

1 Answers1

6

You're throwing the exception by value, which is indeed recommended practice. However, it means the exception gets copied as part of the throw statement, so it must have an accessible copy constructor. And because you have a non-copyable member (std::stringstream), you must provide your own copy ctor.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • Or could I just declare the `stringstream` as `static` to prevent copying? – mchen May 13 '13 at 06:25
  • @MiloChen Well, this would mean all instances of `myCout` share the one stream, so you'd have to 1) clear it before each use, and 2) introduce some synchronization for mutlithreaded access. If you have C++11, you could sove 2) by making it `static thread_local`. – Angew is no longer proud of SO May 13 '13 at 06:44
  • 2
    You could have a `stringstream` local to the function, and keep the results in a `string` member variable. I know it's less efficient, but throwing an exception should be exceptional. Or you could have a member pointer to a `stringstream`. – Peter Wood May 13 '13 at 07:00