zenkaon
February 14th, 2008, 09:37 PM
Hello all,
I have a problem I was hoping someone could shed some light on.
I have many objects of the same type, so I usually put them all in a stl vector and then iterate on that vector when I want to do a common thing to all of the objects.
Except if my objects have a stringstream private member variable I am unable to do so. This is bloody annoying as it is bloody handy if an object knows its own name.
Below is a simplified example of my problem:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using std::vector;
using std::stringstream;
using std::cout;
using std::endl;
class foo{
public:
explicit foo(stringstream* t);
~foo();
void sayhello();
private:
stringstream text_; // (1)
};
foo::foo(stringstream* t){
text_<<t->str(); // (2)
}
foo::~foo(){}
void foo::sayhello(){
cout<<text_.str()<<endl; // (3)
}
int main(){
stringstream* hello=new stringstream;
*hello<<"Hello World !";
foo* bar = new foo(hello);
delete hello;
bar->sayhello();
vector<foo>* v_foo = new vector<foo>;
//v_foo->push_back(*bar); // (4)
delete bar;
delete v_foo;
return EXIT_SUCCESS;
}
At present, this code will compile and runs fine.
However, uncomment (4) and I get the compiler error message:
I am aware that I am using a red-hat system and posting on an ubuntu forum. I've moved to ubuntu, the sys-admins at work love ubuntu, but work remains steadfastly RHEL. I'm not going to be able to change my system or compiler.
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.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/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h:81: instantiated from `void std::_Construct(_T1*, const _T2&) [with _T1 = foo, _T2 = foo]'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_vector.h:560: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = foo, _Alloc = std::allocator<foo>]'
foo.cpp:39: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/ios_base.h:781: error: `std::ios_base::ios_base(const std::ios_base&)' is private
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h:81: error: within this context
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h: 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/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h:81: instantiated from `void std::_Construct(_T1*, const _T2&) [with _T1 = foo, _T2 = foo]'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_vector.h:560: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = foo, _Alloc = std::allocator<foo>]'
foo.cpp:39: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/streambuf:769: 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/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h:81: error: within this context
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc: In member function `std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc:238: instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = foo, _Alloc = std::allocator<foo>]'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_vector.h:564: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = foo, _Alloc = std::allocator<foo>]'
foo.cpp:39: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/ios_base.h:784: error: `std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc:238: error: within this context
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc: In member function `std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >& std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::operator=(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)':
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/streambuf:776: error: `std::basic_streambuf<_CharT, _Traits>& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc:238: error: within this context
Now if (1), (2) & (3) are commented out, with (4) uncommented, the code will compile and run once more.
I have tried this using references as well as pointers to the stringstreams.
All other types of data member seem to work, it seems specific to stringstreams.
Any ideas as to what's going on?
I have a problem I was hoping someone could shed some light on.
I have many objects of the same type, so I usually put them all in a stl vector and then iterate on that vector when I want to do a common thing to all of the objects.
Except if my objects have a stringstream private member variable I am unable to do so. This is bloody annoying as it is bloody handy if an object knows its own name.
Below is a simplified example of my problem:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using std::vector;
using std::stringstream;
using std::cout;
using std::endl;
class foo{
public:
explicit foo(stringstream* t);
~foo();
void sayhello();
private:
stringstream text_; // (1)
};
foo::foo(stringstream* t){
text_<<t->str(); // (2)
}
foo::~foo(){}
void foo::sayhello(){
cout<<text_.str()<<endl; // (3)
}
int main(){
stringstream* hello=new stringstream;
*hello<<"Hello World !";
foo* bar = new foo(hello);
delete hello;
bar->sayhello();
vector<foo>* v_foo = new vector<foo>;
//v_foo->push_back(*bar); // (4)
delete bar;
delete v_foo;
return EXIT_SUCCESS;
}
At present, this code will compile and runs fine.
However, uncomment (4) and I get the compiler error message:
I am aware that I am using a red-hat system and posting on an ubuntu forum. I've moved to ubuntu, the sys-admins at work love ubuntu, but work remains steadfastly RHEL. I'm not going to be able to change my system or compiler.
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.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/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h:81: instantiated from `void std::_Construct(_T1*, const _T2&) [with _T1 = foo, _T2 = foo]'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_vector.h:560: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = foo, _Alloc = std::allocator<foo>]'
foo.cpp:39: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/ios_base.h:781: error: `std::ios_base::ios_base(const std::ios_base&)' is private
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h:81: error: within this context
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h: 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/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h:81: instantiated from `void std::_Construct(_T1*, const _T2&) [with _T1 = foo, _T2 = foo]'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_vector.h:560: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = foo, _Alloc = std::allocator<foo>]'
foo.cpp:39: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/streambuf:769: 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/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_construct.h:81: error: within this context
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc: In member function `std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char, std::char_traits<char> >::operator=(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc:238: instantiated from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = foo, _Alloc = std::allocator<foo>]'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_vector.h:564: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = foo, _Alloc = std::allocator<foo>]'
foo.cpp:39: instantiated from here
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/ios_base.h:784: error: `std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc:238: error: within this context
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc: In member function `std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >& std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::operator=(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)':
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/streambuf:776: error: `std::basic_streambuf<_CharT, _Traits>& std::basic_streambuf<_CharT, _Traits>::operator=(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/vector.tcc:238: error: within this context
Now if (1), (2) & (3) are commented out, with (4) uncommented, the code will compile and run once more.
I have tried this using references as well as pointers to the stringstreams.
All other types of data member seem to work, it seems specific to stringstreams.
Any ideas as to what's going on?