By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

vector.resize assignment operator problem

P: n/a
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<MyObjfirst(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<<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: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_iterat or<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<typen ame
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::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<<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::operator=(const MyObj&)’ first required here
Jun 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bram Kuijper wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '07 #2

P: n/a
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
Jun 13 '07 #3

P: n/a
"Bram Kuijper" <a.***********@rug.nlwrote in message
news:f4**********@info.service.rug.nl...
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<MyObjfirst(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::objref’, 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::operator=(const MyObj&)’ first required here

Jun 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.