473,664 Members | 3,022 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 3736
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
3423
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
3172
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
3989
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
5151
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
2273
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 same quantity in output. To make this, I've supposed to can modify the operator() for the access at...
1
3956
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 overloaded assignment operation is unable to recognize the
14
9765
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
2165
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, using the standard algorithm for_each.
9
1949
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 = build_recursive(n-1); b.left = x;
0
8437
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8348
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8549
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7375
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4185
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
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
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.