473,508 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector.resize assignment operator problem

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
3 3715
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3414
by: Marc Schellens | last post by:
I have (or better had) a class wich contains a fstream (as a private member). I made a vector of this class. When I resize() the vector I got several error messages, about some stuff being...
9
3155
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
14
3979
by: Michael Sgier | last post by:
Hello If someone could explain the code below to me would be great. // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); }...
10
5121
by: vsgdp | last post by:
On Page 67 of Effective STL, Meyers writes about resize: "If n is smaller than the current size, elements at the end of the container will be destroyed." What does "destroyed" mean? It seems to...
5
2258
by: Toto | last post by:
Hello, I've got a problem with the redefinition of operator in C++. My intent is to create a TVector that internally work with __int64, with an external interface of double in order to optimize...
1
3947
by: Raghuram N K | last post by:
Hi, Following program compiles and executes successfully in windows with DevCPP compiler. When I compile the same in Linux with 'g++323' compiler I get following assignment error: cannot...
14
9734
by: Tarun | last post by:
Hello, I am facing problem sometimes while I am trying to do push_back on a vector. Currently I am doing resize of the vector increasing the size by one and then push_back and seems like the...
3
2160
by: Eric Lilja | last post by:
Hello, consider the following assignment and my code for it: /* Write a program that does the following: * Integer numbers shall be read from a textfile and stored in a std::vector. The name...
9
1938
by: xman | last post by:
The codes below basically builds a binary tree. It runs well on Intel compiler. However, when I use gcc 4.2.0, the assignment to b.right causes segmentation fault. Tracing with valgrind reveals...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.