I am trying to resize a vector of objects (MyObj below), which contain
references to other objects (OtherObj, see below). However, apparently
somewhere in the resize operation an assignment is done of the
referenced OtherObj object (according to the compiler messages). This is
strange, since the referenced OtherObj is initialized using member
initialization lists. Anyone a clue?
thanks,
Bram Kuijper
this is the source. Compiler messages (g++ 4.1.3) are listed below.
#include<vector >
using namespace std;
class OtherObj
{
public:
OtherObj()
{}
};
class MyObj
{
private:
OtherObj const &objref;
public:
MyObj(OtherObj const &obj)
:
objref(obj)
{}
MyObj(MyObj const &other)
:
objref(other.ob jref)
{}
};
int main()
{
OtherObj obj = OtherObj(); //fine
vector<MyObjfir st(3, MyObj(obj)); //okay
first.resize(10 , MyObj(obj)); //ERROR
return 0;
}
test.cpp: In member function ‘MyObj& MyObj::operator =(const MyObj&)’:
test.cpp:14: instantiated from ‘static void std::__fill<<an onymous>
>::fill(_Forwar dIterator, _ForwardIterato r, const _Tp&) [with_ForwardIterato r = __gnu_cxx::__no rmal_iterator<M yObj*,
std::vector<MyO bj, std::allocator< MyObj >, _Tp = MyObj, bool
<anonymous= false]’
/usr/include/c++/4.1.3/bits/stl_algobase.h: 568: instantiated from
‘void std::fill(_Forw ardIterator, _ForwardIterato r, const _Tp&) [with
_ForwardIterato r = __gnu_cxx::__no rmal_iterator<M yObj*,
std::vector<MyO bj, std::allocator< MyObj >, _Tp = MyObj]’
/usr/include/c++/4.1.3/bits/vector.tcc:330: instantiated from ‘void
std::vector<_Tp ,
_Alloc>::_M_fil l_insert(__gnu_ cxx::__normal_i terator<typenam e
std::_Vector_ba se<_Tp, _Alloc>::_Tp_al loc_type::point er,
std::vector<_Tp , _Alloc, size_t, const _Tp&) [with _Tp = MyObj,
_Alloc = std::allocator< MyObj>]’
/usr/include/c++/4.1.3/bits/stl_vector.h:65 8: instantiated from ‘void
std::vector<_Tp , _Alloc>::insert (__gnu_cxx::__n ormal_iterator< typename
std::_Vector_ba se<_Tp, _Alloc>::_Tp_al loc_type::point er,
std::vector<_Tp , _Alloc, size_t, const _Tp&) [with _Tp = MyObj,
_Alloc = std::allocator< MyObj>]’
/usr/include/c++/4.1.3/bits/stl_vector.h:42 6: instantiated from ‘void
std::vector<_Tp , _Alloc>::resize (size_t, _Tp) [with _Tp = MyObj, _Alloc
= std::allocator< MyObj>]’
test.cpp:36: instantiated from here
test.cpp:14: error: non-static reference member ‘const OtherObj&
MyObj::objref’, can't use default assignment operator
*** Especially this error message above is puzzling me ***
/usr/include/c++/4.1.3/bits/stl_algobase.h: In static member function
‘static void std::__fill<<an onymous::fill(_ ForwardIterator ,
_ForwardIterato r, const _Tp&) [with _ForwardIterato r =
__gnu_cxx::__no rmal_iterator<M yObj*, std::vector<MyO bj,
std::allocator< MyObj >, _Tp = MyObj, bool <anonymous= false]’:
/usr/include/c++/4.1.3/bits/stl_algobase.h: 529: note: synthesized method
‘MyObj& MyObj::operator =(const MyObj&)’ first required here