0

In order to simplify matters, I wrote this class StringStream in a Notifications.h file:

namespace A {
namespace B {
namespace C {

extern std::locale g_classicLocale;
class StringStream : public virtual std::ostringstream
{
 public:
        StringStream() { imbue(g_classicLocale); }
        virtual ~StringStream() {};
};

} // namespace C
} // namespace B
} // namespace A

Now, I'm trying to use StringStream in my DBNotification.cpp file:

 #include "Notifications.h"

 namespace A{
 namespace B{
 namespace C{

         DBNotification::DBNotification(){

         }
         DBNotification::~DBNotification(){

         }
         StringStream DBNotification::getValues(){

            // do some work and return me a StringStream Object
         }
 } // C
 } // B
 } // A

When I build the source code, I get:

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h: In copy constructor 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h:779: error: 'std::ios_base::ios_base(const std::ios_base&)' is private
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:55: error: within this context
../Notifications.h: In copy constructor 'A::B::C::StringStream::StringStream(const A::B::C::StringStream&)':
../Notifications.h:49: note: synthesized method 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)' first required here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/streambuf: In copy constructor 'std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/streambuf:781: error: 'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:71: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd: In copy constructor 'std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::basic_ostringstream(const std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >&)':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:79: note: synthesized method 'std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)' first required here

Could someone let me know what's going on?

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
cybertextron
  • 10,547
  • 28
  • 104
  • 208
  • You simply can not inherit streams. You could inherit [`std::basic_streambuf`](http://en.cppreference.com/w/cpp/io/basic_streambuf) and use it when constructing a new stream. – Some programmer dude Aug 08 '12 at 13:21
  • @JoachimPileborg in the same way, I mean, `StringStream os; os << "helloworld" << endl;` ? – cybertextron Aug 08 '12 at 13:23
  • possible duplicate of [Why copying stringstream is not allowed?](http://stackoverflow.com/questions/6010864/why-copying-stringstream-is-not-allowed) – ecatmur Aug 08 '12 at 13:26
  • @ecatmur Totally unrelated question ... here I'm trying to extend `std::ostringstream` – cybertextron Aug 08 '12 at 13:32
  • @philippe you're copying the `std::ostringstream` when you return it, which is why you're getting that error. – ecatmur Aug 08 '12 at 13:39
  • @ecatmur I solved the problem by passing a `StringStream& os` as a parameter to the function. Could you make your question an answer? – cybertextron Aug 08 '12 at 14:03
  • @philippe [PermanentGuest's answer](http://stackoverflow.com/a/11865534/567292) is correct. – ecatmur Aug 08 '12 at 14:19

1 Answers1

3

Generally streams cannot be copied. Your function DBNotification::getValues() returns a stream object.

Please see this answer

Community
  • 1
  • 1
PermanentGuest
  • 5,213
  • 2
  • 27
  • 36