vector.resize assignment operator problem

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::eek:perator=(const MyObj&)’:
test.cpp:14: instantiated from ‘static void std::__fill said:
>::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: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::pointer,
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::pointer,
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::eek: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::eek:perator=(const MyObj&)’ first required here
 
V

Victor Bazarov

Bram said:
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?

One of the requirements for the contained type ('MyObj' in your case) is
that it is *Assignable*. You need to provide your user-defined operator
if the compiler is unable to generate one for you.

It is up to you how you do it. You will not be able to re-seat the
reference member to refer to the other object's referee, so if you need
the assignment to change that, you should consider storing a pointer
to 'OtherObj' in 'MyObj' instead.

V
 
M

Markus Moll

Hi

Bram Kuijper schreef:
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,

You are violating the container requirements:

"The type of objects stored in these components must meet the
requirements of CopyConstructible types (20.1.3), and the additional
requirements of Assignable types."

Markus
 
J

Jim Langston

Bram Kuijper said:
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;
} [snip bunch of error deetail]
test.cpp:14: error: non-static reference member ‘const OtherObj&
MyObj::eek:bjref’, can't use default assignment operator

OtherObj& is a reference. A reference in your class can't be assigned by
the default assignment operator. References in a class need to be
initialized, not assigned, that's where the problem is coming in.
*** 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::eek:perator=(const MyObj&)’ first required here
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top