B
Bram Kuijper
Hi all,
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.objref)
{}
};
int main()
{
OtherObj obj = OtherObj(); //fine
vector<MyObj> first(3, MyObj(obj)); //okay
first.resize(10, MyObj(obj)); //ERROR
return 0;
}
test.cpp: In member function ‘MyObj& MyObj:perator=(const MyObj&)’:
std::vector<MyObj, std::allocator<MyObj> > >, _Tp = MyObj, bool
<anonymous> = false]’
/usr/include/c++/4.1.3/bits/stl_algobase.h:568: instantiated from
‘void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with
_ForwardIterator = __gnu_cxx::__normal_iterator<MyObj*,
std::vector<MyObj, std::allocator<MyObj> > >, _Tp = MyObj]’
/usr/include/c++/4.1.3/bits/vector.tcc:330: instantiated from ‘void
std::vector<_Tp,
_Alloc>::_M_fill_insert(__gnu_cxx::__normal_iterator<typename
std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type:ointer,
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:658: instantiated from ‘void
std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename
std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type:ointer,
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:426: 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:bjref’, 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<<anonymous> >::fill(_ForwardIterator,
_ForwardIterator, const _Tp&) [with _ForwardIterator =
__gnu_cxx::__normal_iterator<MyObj*, std::vector<MyObj,
std::allocator<MyObj> > >, _Tp = MyObj, bool <anonymous> = false]’:
/usr/include/c++/4.1.3/bits/stl_algobase.h:529: note: synthesized method
‘MyObj& MyObj:perator=(const MyObj&)’ first required here
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.objref)
{}
};
int main()
{
OtherObj obj = OtherObj(); //fine
vector<MyObj> first(3, MyObj(obj)); //okay
first.resize(10, MyObj(obj)); //ERROR
return 0;
}
test.cpp: In member function ‘MyObj& MyObj:perator=(const MyObj&)’:
_ForwardIterator = __gnu_cxx::__normal_iterator<MyObj*,test.cpp:14: instantiated from ‘static void std::__fill said:>::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with
std::vector<MyObj, std::allocator<MyObj> > >, _Tp = MyObj, bool
<anonymous> = false]’
/usr/include/c++/4.1.3/bits/stl_algobase.h:568: instantiated from
‘void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with
_ForwardIterator = __gnu_cxx::__normal_iterator<MyObj*,
std::vector<MyObj, std::allocator<MyObj> > >, _Tp = MyObj]’
/usr/include/c++/4.1.3/bits/vector.tcc:330: instantiated from ‘void
std::vector<_Tp,
_Alloc>::_M_fill_insert(__gnu_cxx::__normal_iterator<typename
std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type:ointer,
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:658: instantiated from ‘void
std::vector<_Tp, _Alloc>::insert(__gnu_cxx::__normal_iterator<typename
std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type:ointer,
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:426: 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:bjref’, 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<<anonymous> >::fill(_ForwardIterator,
_ForwardIterator, const _Tp&) [with _ForwardIterator =
__gnu_cxx::__normal_iterator<MyObj*, std::vector<MyObj,
std::allocator<MyObj> > >, _Tp = MyObj, bool <anonymous> = false]’:
/usr/include/c++/4.1.3/bits/stl_algobase.h:529: note: synthesized method
‘MyObj& MyObj:perator=(const MyObj&)’ first required here