tags:

views:

157

answers:

3

What i want to do is

for (list<cPacket *>::iterator i = cache.begin(); i != cache.end(); i++){
        if( strcmp(i->getName(),id) == 0 ){
            return true;
        }
}

where getName is function of the class cPacket, But it does not work, i tries also i.operator->()->getName(), and again nothing.

Can anybody help me?

+5  A: 
(*i)->getName()

is what you are looking for.

Niki Yoshiuchi
That still won't work without a cast.
sblom
yes, thanks a lot. Can you tell me what (*i) exactrly mean
pavlos
@pavlos: You can think of the iterator as a pointer to an element of your container. But in this case, the elements in the container are pointers. `(*i)` will give you the pointer from your container, and then you can use `->getName()` to call the function on the object it's pointing to.
Fred Larson
*i dereferences a pointer, or in this case, dereferences an iterator. It's very similar to the -> operator - instead of ptr->getName(), you could also do (*ptr).getName(). In this case, since you have an iterator containing a pointer, you first dereference the iterator using *, then dereference the pointer using ->.
Niki Yoshiuchi
sorry again, but i tried as you said, and then add this lines:void ProxyServer::addURLToCache( cPacket *url ){ if( numOfUrlsInCache < 3 ){ cache.push_back( url ); numOfUrlsInCache++; } else EV<<"Cache is full"<<endl;}and then when i try to iterate again it crashes! What is this about.
pavlos
It's hard to tell without seeing more of your code. It could be that *url is NULL or an invalid pointer. Trying to dereference a null pointer can cause a crash.
Niki Yoshiuchi
+4  A: 

*i dereferences the iterator. As the data type of the list is pointer to cPacket, you need to apply the -> operator to access its members. Parentheses are needed for proper precendence:

(*i)->whatever()
Alexander Gessler
A: 

replace

list<cPacket *>::iterator i

with

list<cPacket>*::iterator i
sp3tsnaz
The type `list<cPacket>*::iterator` is syntactically illegal, not to mention the fact that it is not equivalent to the return type of `cache.begin()` and `cache.end()`. That's two reasons why it won't compile.
Ken Bloom