• Advertisement
Sign in to follow this  

issue with removing point from a list

This topic is 4040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a list and the type is a struct I made. but when I try the following code I get error no matching function for call to `remove(std::_List_iterator<SPRITE>, std::_List_iterator<SPRITE>, POINT*, std::_List_iterator<SPRITE>)' note C:\Program Files\Dev-Cpp\include\stdio.h:183 candidates are: int remove(const char*) void Remove_list(POINT pos, RECT Src) { int nums_iter; for(list<SPRITE>::iterator i=Sprites.begin();i!=Sprites.end();++i) { Sprites.erase( remove( Sprites.begin(), Sprites.end(),&i->Pos, Sprites.end() ); } } How can I search through the list that has a my struct as the type and find a specific member and remove it?

Share this post


Link to post
Share on other sites
Advertisement
you have to test the iterator to see if it equals the sprite that you want to remove. i.e.

void Remove_list(SPRITE sprite)
{
for(list<SPRITE>::iterator i=Sprites.begin();i!=Sprites.end();++i)
{
if (sprite == *i)
Sprites.erase( i );

}
}


However, I don't know what the POINT pos and RECT Src information hold, but if they are a part of the sprite that you need to remove maybe this would work

void Remove_list(POINT pos, RECT Src)
{
for(list<SPRITE>::iterator i=Sprites.begin();i!=Sprites.end();++i)
{
if (pos == i->pos && Src == i->Src)
Sprites.erase( i );

}
}


Also, what is int nums_iter; doing?

[Edited by - MikeTacular on January 27, 2007 10:45:47 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
you have to test the iterator to see if it equals the sprite that you want to remove. i.e.

*** Source Snippet Removed ***
However, I don't know what the POINT pos and RECT Src information hold, but if they are a part of the sprite that you need to remove maybe this would work

*** Source Snippet Removed ***
Also, what is int nums_iter; doing?


I forgot to earse int nums_iter; .
second when I try that I get error
'struct SPRITE' has no member named 'pos'

So I think the issue is how to remove iteams from the list that is using a struct call SPRITE?

Share this post


Link to post
Share on other sites
Oops I made a mistake in the code, okay it's fixed now. In void Remove_list(POINT pos, RECT Src) what do pos and Src hold information about? It doesn't look like you are using them at all in the function. And second remember that in my first post that I said
Quote:
However, I don't know what the POINT pos and RECT Src information hold, but if they are a part of the sprite that you need to remove maybe this would work
Which means that you may have to play with the code to get it to work since I don't know what a SPRITE is defined as and what pos and Src tell you.

I noticed you didn't say that the compiler complained about Src == i->Src which suggests to me that maybe pos == i->Pos would work.

Remember that to remove something from a list, you must iterate through it (which you are doing), test the iterator and see if it is the item you want to remove (which was missing from your code), and then tell the list to erase(iterator).

Anyways, hoped that helped.

[EDIT]
I would suggest looking at these two tutorials:
Clicky 1
Clicky 2

[Edited by - MikeTacular on January 27, 2007 11:43:14 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular

what do pos and Src hold information about?



it is loaded from this. It holds the rect and x and y coords for the block


list<int>::iterator curr = DataList.begin();

list<LoadData::RectX>::iterator curr2 = DataList2.begin();

while(curr != DataList.end()&& curr2 !=DataList2.end())
{
// the position
PoX = *curr;
++curr;
PoY = *curr;
++curr;

left = curr2->left;
top = curr2->top;
right = curr2->right;
bottom = curr2->bottom;
++curr2;

Add(MakePoint(PoX,PoY),MakeRect(left,top,right, bottom));


Quote:
Original post by MikeTacular

I don't know what a SPRITE is defined as and what pos and Src tell you.



SPRITE is defined here


typedef struct {
int x,y;
int width,height;
int movex,movey;
int curframe,lastframe;
int animdelay,animcount;
int scalex, scaley;
int rotation, rotaterate;
POINT Pos;
RECT Src;
} SPRITE;




Share this post


Link to post
Share on other sites
How do I compare the iterator with the pos?

if(paddle.Pos == *i)


I get error
no match for 'operator==' in 'paddle.SPRITE::Pos == (&i)->std::_List_iterator<_Tp>::operator* [with _Tp = SPRITE]()'

note C:\Program Files\Dev-Cpp\include\objbase.h:80 candidates are: BOOL operator==(const GUID&, const GUID&)


no matter what I try I get the simalr error

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
How do I compare the iterator with the pos?

If the iterator is to a list<SPRITE>, which seems to be the case, you must dereference the iterator with the * operator, effectively turning it into a SPRITE. Then, you can access its members:
if(paddle.Pos == (*i).Pos)
-jouley

[Edit: vector to list]

Share this post


Link to post
Share on other sites
still get the same exact error. I have been at this for a wile and this little thing is killing me. I just dont understand why I cant compare the data I put into it with the data inside the list

Share this post


Link to post
Share on other sites
POINT is probably a struct and doesn't have any operator overloads. The compiler doesn't know what to do with it either.

You will have to compare the member variables of the point structures instead.

if(paddle.Pos.x == (*i).Pos.x && paddle.Pos.y == (*i).Pos.y)

Share this post


Link to post
Share on other sites
Ah, I should have read more carefully. There's no '==' operator for POINTs. So, you'll have to examine both members independently:
if(paddle.Pos.x == (*i).Pos.x && paddle.Pos.y == (*i).Pos.y)


[Edit: beated]

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
still get the same exact error. I have been at this for a wile and this little thing is killing me. I just dont understand why I cant compare the data I put into it with the data inside the list
Can you post your code exactly as it is now, incorporating any revisions that you've made? Also make sure to show the declarations of any variables used in the examples so that we know their types.

Keep in mind that types cannot be mixed, compared, assigned, or what have you in an arbitrary way in C++; whether you view C++ as a strongly-typed or weakly-typed language, there are rules for what sort of conversions can occur and when. When writing C++ code, you can't just follow your intuition - you have to look carefully at the types involved, consider any implicit conversion that might or might not be supported, and construct the syntax accordingly.

Again, I'd recommend posting your revised code (along with any error messages that you're getting) so that we can take another look at it.

[Edit: This post may be irrelevant at this point - or maybe not :)]

Share this post


Link to post
Share on other sites
Quote:
Original post by jouley
Ah, I should have read more carefully. There's no '==' operator for POINTs. So, you'll have to examine both members independently:
if(paddle.Pos.x == (*i).Pos.x && paddle.Pos.y == (*i).Pos.y)


[Edit: beated]


All I have to say is wow how and why in the world did that work. It does exactley what I need it to do. But I dont understand how.

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
Quote:
Original post by jouley
Ah, I should have read more carefully. There's no '==' operator for POINTs. So, you'll have to examine both members independently:
if(paddle.Pos.x == (*i).Pos.x && paddle.Pos.y == (*i).Pos.y)


[Edit: beated]


All I have to say is wow how and why in the world did that work. It does exactley what I need it to do. But I dont understand how.

This is where what jyk said comes in (not that it didn't need to come in before). The data types you were trying to compare weren't the same. In much the same way that you can't compare apples to oranges ("But they're both fruits!"), you can't compare a POINT struct to a list::<SPRITE>::iterator ("But they're both data types!").

As to why you have to compare both members of the POINT, it's because there's no equality operator defined for the struct. When I said
Quote:
if(paddle.Pos == (*i).Pos)
I didn't take this into account. The compiler doesn't know how to compare two POINTs. While it's obvious to you ("If both the x and y members are the same, then the POINTs are equal."), the compiler doesn't understand this. Since this wasn't defined when POINT was, you have to tell the compiler explicitly whenever you want to compare two of them.

-jouley

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
Quote:
Original post by jouley
Ah, I should have read more carefully. There's no '==' operator for POINTs. So, you'll have to examine both members independently:
if(paddle.Pos.x == (*i).Pos.x && paddle.Pos.y == (*i).Pos.y)


[Edit: beated]


All I have to say is wow how and why in the world did that work. It does exactley what I need it to do. But I dont understand how.
It works because it does exactly what it says it does :) First it compares paddle.Pos.x to i->Pos.x, where 'i' is an iterator pointing to (I gather) a 'sprite' struct. Note that (*i).Pos.x and i->Pos.x are functionally equivalent here.

If the first conditional passes (the x components are in fact equal), the second conditional is evaluated. If the y components of the positions are also equal, then we consider the positions to be equal and the compound conditional passes. The compound conditional is evaluated in this way due to the use of &&, the logical AND operator. You can also create compound conditionals using logical OR (||), or various combinations of && and ||.

What jouley was alluding to earlier was that this syntax is only necessary because the Point struct itself does not have an overloaded operator==(), so you cannot compare Points directly as you might intuitively expect. Implementing such an operator would be a good idea, as it would be less error-prone and considerably more convenient.

I'm not sure if that addresses your question(s) at all; if not, perhaps you can clarify.

[Edit: Basically just a restatement of what jouley said, above.]

Share this post


Link to post
Share on other sites
Thanks so very much. It makes sence now why it was not working.

So how would you implement overloaded operator==(). Also I am not sure what that means. I thought == just compares values but then again like jouley and jyk said there were differnt types of veribles I was trying to compare. But to me I thought they were the same values. so how do you find out if there differnt kinds of data to beginwith? It might be obvouse to some but to otheres they may thinck the same thing I thoght.

Share this post


Link to post
Share on other sites
Just a tutorial I found in operator overloading , just by looking in google.If you have a book you are following though in C++ , I would suggest you check it there, because it must be better for you and/or more familiar.

Ok as for what you asked.Imagine that in C++ the operators == , = , + , - e.t.c. ( they are many :p won't mention them all ) are used only for the basic c++ data types , like int , float , double e.t.c.


You were comparing your 2 objects of your point class.The compiler could not understand what == would do between 2 Points.Ok == is equality but the compiler can not understand how to compare 2 Points.When would 2 Points be equal?

That's where operator overloading comes in handy.Just follow the tutorial in the link above and just overload the == operator for the Point class to check the x and y of the point.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement