Problem with STL map iterators

Started by
9 comments, last by Hazelnuss 20 years, 11 months ago
Does anybody have an idea why i can't do this?
        
void CResourceManager::ReleaseAll()
{
	std::map<int, int>::iterator iter;
	for(iter = m_References.begin(); iter != m_References.end(); ++iter)
	{
		while(iter.second)
			Release(iter.first);
	}
}
        
The compiler says that "second" is not an element of
'_Rb_tree_iterator,struct _STL::_Nonconst_traits > >'  
I am using STLPort. Thanks for any help. [edited by - Hazelnuss on May 28, 2003 11:51:45 AM] [edited by - Hazelnuss on May 28, 2003 11:52:17 AM] [edited by - Hazelnuss on May 28, 2003 11:52:47 AM]
Advertisement
i dont use stl, so my ideas can be crap... anyways, perhaps the second and first are methods? not variables? 
I am a signature virus. Please add me to your signature so that I may multiply.
Use a temporary variable to hold the map''s key and/or value.

int nTemp = iter.second;

Kuphryn
The iterator doesn't have those as members. The thing the iterator is pointing to does.

You need to derefernce the iterator first to get the std:: pair which has first and second as members.

(*iter).first;

[edited by - petewood on May 28, 2003 12:06:22 PM]
quote:Original post by pag
i dont use stl, so my ideas can be crap...    

Agreed JK

Solution. iterator are classes and you must use the dereference
operator to access their content like this:


    void CResourceManager::ReleaseAll(){	     std::map<int, int>::iterator iter, end; // avoid redundant calls     for(iter = m_References.begin(), end = m_References.end(); iter != end; ++iter)     {       int temp = iter->second;    // method 1)       while(temp--)               // watch out for infinitive loops!	  Release((*iter).first);  // method 2)     }}    


[edit]
Seems I was a little too slow
[/edit]




[edited by - darookie on May 28, 2003 12:09:03 PM]
Hazelnuss: I just noticed that you try to release the same resource multiple times - is this really what you intended
to do?

Another thing, after ReleaseAll() you should also clear the reference map:

      m_References.clear();  

quote:Original post by kuphryn
Use a temporary variable to hold the map''s key and/or value.

int nTemp = iter.second;

Kuphryn


Why ?
Thanks for your answers thus far! I am gonna try them out.

quote:Original post by darookie
Hazelnuss: I just noticed that you try to release the same resource multiple times - is this really what you intended
to do?

Another thing, after ReleaseAll() you should also clear the reference map:



Yep, that's what i want. I am working with reference counters and each Release() decrements it. When it is equal 0 the resource is deleted from memory. And i am calling ::clear() in the destructor because i might want to reload the resources after calling ReleaseAll().

[edited by - Hazelnuss on May 28, 2003 1:10:13 PM]
Thanks, it works perfectly now.
It drove me crazy, VC++ told me it were valid members when i typed the ".".
Try "iter->second"

This topic is closed to new replies.

Advertisement