I've been experimenting with C++, and I've come across a problem that I don't know how to solve.
Basically, I've discovered that you can't copy streams (see Why copying stringstream is not allowed?), and that also applies for objects that 'wrap' them. For example:
- I create a class with a data member of type stringstream.
- I create an object of this class.
- I attempt to copy the object, eg "TestObj t1; TestObj t2; t1 = t2;"
This causes the error C2249:
'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
So my question is: how can I (preferably easily) copy objects that have data members of type *stream?
Full example code:
#include <iostream>
#include <string>
#include <sstream>
class TestStream
{
public:
std::stringstream str;
};
int main()
{
TestStream test;
TestStream test2;
test = test2;
system("pause");
return 0;
}
Thanks in advance.
UPDATE
I've managed to solve this problem thanks the answers below. What I have done is declare the stream objects once and then simply reference them using pointers in the wrapper objects (eg, TestStream). The same goes for all other objects that have private copy constructors.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…