473,770 Members | 5,977 Online
Bytes | Software Development & Data Engineering Community
+ 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>::it erator iter = room->living.begin() ;
while (iter != room->living.end() )
{
if (*iter == *this)
{
room->living.erase(i ter);
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 2964

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>::it erator iter = room->living.begin() ;
while (iter != room->living.end() )
{
if (*iter == *this)
{
room->living.erase(i ter);
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>::it erator iter = room->living.begin() ;
while (iter != room->living.end() )
{
if (*iter == *this)
{
room->living.erase(i ter);
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.co m> <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 misunderstandin g 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 misunderstandin g 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<Table Item*> 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<Table Item> 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
1812
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" Response.cookies("items")("item3") = "333333" Is there a way I can remove items- item2 and rearrange above cookie into Response.cookies("items")("item1") = "111111"
2
8424
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. ---------------------------------------------------------------------------- ------------------ #include <iostream> #include <list> using namespace std;
6
6663
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 give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
4
3853
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) linked list to continousely (dynamically) free up more memory when needed and put the buffers with the data of the file on a sort of stack. The problem is that i have no idea how to do this.
3
2626
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 show up in my Add/Remove componenets list when I try to add it to the toolbar?
0
5296
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 page instance. Neat trick, I think... #Boa:Wizard:Wizard1 import wx import wx.wizard import SetupWizardPage1 import SetupWizardPage2
3
2133
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 responseXML. This fetches item list in Mozilla and Opera, however it creates errors in IE.
7
22700
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
2207
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 ) { vector<T>::iterator i; // ERROR, can't use iterator }
3
2660
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 when the List is of Type String but fails when it's any thing other e.g Rectangle any help is kindly appreciated
0
9592
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
9425
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,...
0
10059
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
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
8887
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
7416
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
6679
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3972
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
3
2817
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.