Original Post
I'm having a problem with copying vectors. So I have this vector of objects call it std:: vector first_vector. parent is the base class. The problem is I want to create a seperate vector std::vector second_vector that gets all the information copied from first_vector.
The problem is when I copy it like this
it copies only the adress of the pointers in first_vector so when I change one it changes the other. I needed to know How do I deep copy vector with pointers such that they are independant
So I did some more searching and found This aricle expalining how to clone an object. I followed the directives exactly since I need polymorphic copying. so now I am copying like this
But now I get this error:
c:\program files\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
1> ]
1> c:\program files\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
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,
1> _Alloc=std::allocator
1> ]
I suspect it is because the objects in my vector contain string stream. My question is how do I fix this? How do I clone objects with string stream
The problem is when I copy it like this
second_vector = first_vector; it copies only the adress of the pointers in first_vector so when I change one it changes the other. I needed to know How do I deep copy vector with pointers such that they are independant
So I did some more searching and found This aricle expalining how to clone an object. I followed the directives exactly since I need polymorphic copying. so now I am copying like this
second_vector.resize(first_vector.size());
for(unsigned i = 0; i < first_vector.size(); ++i)
second_vector = first_vector->clone();But now I get this error:
c:\program files\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
1> ]
1> c:\program files\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
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
1> _Alloc=std::allocator
1> ]
I suspect it is because the objects in my vector contain string stream. My question is how do I fix this? How do I clone objects with string stream