473,574 Members | 2,952 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.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
Jun 13 '07 #1
3 3727
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 CopyConstructib le 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.ob jref)
{}
};

int main()
{
OtherObj obj = OtherObj(); //fine

vector<MyObjfir st(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<<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

Jun 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3421
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 private int that context. Is this gcc (3.2) specific or is there a 'official' reason for this? thanks, marc
9
3165
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 WORDS_PER_LINE = 4; when counter == 7 is when the string Concatenation fails within the IF block.
14
3983
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); } // reflect this vector off surface with normal vector
10
5140
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 me that if n is smaller than the current size, simple adjust the internal size counter to n and be done.
5
2269
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 the operations. The double value put in input should be muliplied with factor 1e10, in order to limit the loss of precision and divided with the...
1
3952
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 convert `__gnu_cxx::__normal_iterator<DailyTemp*, std::vector<DailyTemp, std::allocator<DailyTemp >' to `DailyTemp*' in assignment I believe the...
14
9749
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 code is working fine. Is it a better idea to do resize befoire calling push_back? Regards, Tarun
3
2164
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 of the input file is to be given on the command line. * All negative values in the vector shall then be replaced with their positive counterpart,...
9
1944
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 that the particular memory address was deleted during push_back(). If I change the assignments to int x = build_recursive(n-1); int y =...
0
7805
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7726
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7817
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8098
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5301
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3741
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3752
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2245
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.