The online racing simulator
C++ Lists
(3 posts, started )
C++ Lists
Hello, I have a little problem with C++. Im trying to make a connections list, I have used the list in namespace std, or "<list>" if you like. Currently, I can add the connection fine. When it comes to removing the connection I have an issue. The issue being, If I use an iterator to go though the connections list(To find the one to remove) C++ will throw a type of exception.

It says "list iterator not incrementable", This is obivous, As my List is a list of structs.


[SIZE=2][COLOR=#0000ff]struct[/COLOR][/SIZE][SIZE=2] Conn[/SIZE]
[SIZE=2]{ [/SIZE]
[SIZE=2][COLOR=#0000ff] char[/COLOR][/SIZE][SIZE=2] username[24];[/SIZE]
[SIZE=2][COLOR=#0000ff] char[/COLOR][/SIZE][SIZE=2] playername[24];[/SIZE]
[SIZE=2] byte UCID;[/SIZE]
[SIZE=2]};[/SIZE]


[SIZE=2]std::list<Conn> Connections;[/SIZE]

That code may be utter crap, but I have to start somewhere. If you know of a way around this, or a fix. Help me please!

Edit: I shall also add, All the tutorials i found on lists. They used int. Basically that CAN be iterated. So they didnt help.
Something like this should work -
list<Conn>::iterator i = Connections.begin();

while (i != Connections.end())
{
if ((*i)->UCID == whatyourlookingfor)
{
Connections.erase(i);
break;
}
++i;
}

This code assumes that you're just looking for a since item in the list (the if statement probably needs working on. I cant remember if it's a pointer returned or not tbh), and so doesnt deal with dupes.

What you're probably doing is incrementing the iterator after removing an item. Which if you think about it, won't work if you're at the last entry in the list and you've already removed it.

Does that make sense?
Ah, Thank you t_a_a.

Quote from the_angry_angel :What you're probably doing is incrementing the iterator after removing an item. Which if you think about it, won't work if you're at the last entry in the list and you've already removed it.

Turns out, You know me too well . I was not breaking from my loop after I had removed the connection.

Quote from the_angry_angel :Does that make sense?

Does now, Thank you!

C++ Lists
(3 posts, started )
FGED GREDG RDFGDR GSFDG