473,473 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using an iterator to remove an item from a STL list

Hi. I'm having trouble figuring out what I should be doing here.

I'm trying to remove an object from a list.

The function is:
void Alive::FromRoom ()
{
list<Alive>::iterator iter = room->living.begin();
while (iter != room->living.end())
{
if (*iter == *this)
{
room->living.erase(iter);
break;

else ++iter;
}

}

The error message is:
alive.cpp:29: no match for `Alive& == Alive&' operator

which is this line:
if (*iter == *this)

I've declared 'living' here:

class Room : public Baseobj
{
list<Alive> living;
};

So, I'm not sure how I should be doing this. If the error message can
be trusted, I need to define '==' as it applies to the 'Alive' class?
But, I don't understand what that would mean. I mean, I have the object
I want to remove from the list. It's 'this'. So, I don't understand
what more would be needed. The 'this' pointer points to an instance of
an object, doesn' it?

Mar 13 '06 #1
6 2945

Jonathan wrote:
Hi. I'm having trouble figuring out what I should be doing here.

I'm trying to remove an object from a list.

The function is:
void Alive::FromRoom ()
{
list<Alive>::iterator iter = room->living.begin();
while (iter != room->living.end())
{
if (*iter == *this)
{
room->living.erase(iter);
break;

else ++iter;
}

}

The error message is:
alive.cpp:29: no match for `Alive& == Alive&' operator

which is this line:
if (*iter == *this)

I've declared 'living' here:

class Room : public Baseobj
{
list<Alive> living;
};

So, I'm not sure how I should be doing this. If the error message can
be trusted, I need to define '==' as it applies to the 'Alive' class?
But, I don't understand what that would mean. I mean, I have the object
I want to remove from the list. It's 'this'. So, I don't understand
what more would be needed. The 'this' pointer points to an instance of
an object, doesn' it?


You cannot two objects using "==" operator.
(*iter==*this)
Both iter and this when derefernced are objects.
You need to write a function overloading the "==" operator in Alive
class.

Mar 13 '06 #2
Jonathan wrote:
Hi. I'm having trouble figuring out what I should be doing here.

I'm trying to remove an object from a list.

The function is:
void Alive::FromRoom ()
{
list<Alive>::iterator iter = room->living.begin();
while (iter != room->living.end())
{
if (*iter == *this)
{
room->living.erase(iter);
break;

missing '}' here
else ++iter;
}

}

The error message is:
alive.cpp:29: no match for `Alive& == Alive&' operator

which is this line:
if (*iter == *this)


The error says exactly what the problem is. *iter and *this are both of
type Alive (technically, Alive& but that's not essential here) and
you're applying operator== to them without having defined such an operator.

You can define a member function of Alive:
bool Alive::operator== (const Alive& rhs) const {...}
or a nonmember function:
bool operator== (const Alive& lhs, const Alive& rhs) {...}

Alternatively, depending upon your implementation, you might want to
instead compare as:

if (&*iter == this) ...

which is a pointer comparison. This will only work if the object *this
is the same object (i.e., stored in the same location in memory) as the
object in the list. Whether this is the case will depend upon your
specific implementation.

Mark
Mar 13 '06 #3
Jonathan wrote:
I'm trying to remove an object from a list.
However, you problem is [obviously] not related at all to removing
an object from the list. It is related to identifying the object
you want to remove.
If the error message can
be trusted, I need to define '==' as it applies to the 'Alive' class?
I would consider this to be accurate and in general most error
messages can be trusted. At least, it is a good bet to start by
trusting it. Once you have eliminated the possibility that the
error message is true, you might want to seek other reasons.
But, I don't understand what that would mean.
Well, it means that there is no 'operator==()' taking two 'Alive'
objects as arguments. I think, this is pretty straight forward...
I mean, I have the object I want to remove from the list. It's
'this'.
Note, that removing 'this' from the list will also destroy the
object whose method is currently running. This is not by itself
a problem but it is worth noting that removal of 'this' will
render all further accesses to the object invalid.
So, I don't understand what more would be needed.
Your use of 'operator==()' compares the two objects by their values,
not by their identity. To do so, you need to define an 'operator==()'
which takes two 'Alive' objects or reference to such objects as
arguments.
The 'this' pointer points to an instance of
an object, doesn' it?


If you want to compare the object's identities, you would use
something like this:

if (&*iter == this)
...

The subexpression '&*iter' obtains a reference to the object referred
to be 'iter' and then takes this object's address. This address can
readily be compared to 'this'.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 13 '06 #4
Thank you all very much.
if (&*iter == this) is exactly what I needed to know, and all the comments were very
helpful.

But, this brings up a follow-up question. (By the way, would it be more
appropriate to post it by starting a new topic?)
Note, that removing 'this' from the list will also destroy the
object whose method is currently running. This is not by itself
a problem but it is worth noting that removal of 'this' will
render all further accesses to the object invalid.


Again, I seem to be misunderstanding how things work. Using a textbook
type of analogy, let's say I have a STL list of objects that are on
the Table. The Table list includes a Plate, Candlestick, Cup, and a
Cat. Now, I want to remove the 'Cat', from the Table, but I don't have
the slightest desire to destroy the 'Cat'. What's the proper way to
proceed? Make sure that I copy the 'Cat' somewhere else first? Forget
about using the STL, and just make a linked list, C-style?

Mar 13 '06 #5
Jonathan wrote:

Again, I seem to be misunderstanding how things work. Using a textbook
type of analogy, let's say I have a STL list of objects that are on
the Table. The Table list includes a Plate, Candlestick, Cup, and a
Cat. Now, I want to remove the 'Cat', from the Table, but I don't have
the slightest desire to destroy the 'Cat'. What's the proper way to
proceed? Make sure that I copy the 'Cat' somewhere else first? Forget
about using the STL, and just make a linked list, C-style?


Presumably you have a list like so:
std::list<TableItem*> tableItems;

The list does not manage the lifetime of those objects, only of the
pointers to those objects. Removing the pointer does not destroy the
TableItem.

If you have this instead:
std::list<TableItem> tableItems;

Then I'm not sure how they could be of different type (Cat, Plate,
etc.), forgetting that I shall continue:

STL containers rely upon copy-semantics, if it's not in the list, it
doesn't exist. Putting it into the list creates a copy of the Item,
removing from the list removes the copy. In this case, you do not
destroy the (original) cat.

If you want to remove the cat, and keep a copy then that's a different
problem, I suggest you find the cat, copy it and then remove it, as you say.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 13 '06 #6
Thank you. Again, exactly what I needed to put me on the right track.

Mar 14 '06 #7

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

Similar topics

1
by: K. Byanjankar | last post by:
Hi, is there a way to remove item from cookie collection that has a key to it... -- Response.cookies("items")("item1") = "111111" Response.cookies("items")("item2") = "222222"...
2
by: cylin | last post by:
Dear all, How do I change the codes in for-loop to print the value(m_dblXMax) in a list? Please help, thanks. ----------------------------------------------------------------------------...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
3
by: Zorpiedoman | last post by:
Horay! I have just put the finishing touches on a new User Control... The" Jelly Button" I created a setup program which runs fine. I see the .dll nicely in the GAC. How come it does not...
0
bartonc
by: bartonc | last post by:
Examples of wx.Wizard that I found used explicit names for the pages. Using eval() in a list comprehension, I am able to go through the imported modules by name and call create() in order to get a...
3
by: Prashant Mahajan | last post by:
Hi All, I am facing a problem regarding accessing the item list of an XML node in IE. While using the following code: for(test in XMLNode) {} I received this XML Node from AJAX's...
7
by: Donos | last post by:
Hello I have a Queue which is declared as, std::queue<unsigned charm_Queue; I push data into this queue. Now i want to take the data out of this queue using Iterator.
1
by: wuych | last post by:
I have a question about using iterator in template function //*****code starts here***************************** #include <vector> using std::vector; template<typename Tvoid foo( vector<T& a...
3
ThatThatGuy
by: ThatThatGuy | last post by:
How can i search for Rectangle object in a List<T> using BinarySearch() method of List<T> ..... as i'm getting this error failed to compare two elements in a array... the program works fine...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
1
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
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
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...
0
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 ...
1
muto222
php
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.