Archived

This topic is now archived and is closed to further replies.

Hazelnuss

Problem with STL map iterators

Recommended Posts

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]

Share this post


Link to post
Share on other sites

i dont use stl, so my ideas can be crap...
anyways, perhaps the second and first are methods? not variables?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by kuphryn
Use a temporary variable to hold the map''s key and/or value.

int nTemp = iter.second;

Kuphryn


Why ?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
how can you reference count an integer?

edit: i see what you're doing now

[edited by - petewood on May 28, 2003 6:01:58 PM]

Share this post


Link to post
Share on other sites