Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with STL map iterators

Started by Hazelnuss May 28, 2003 at 10:50 AM 9 replies 450+ views
Original Post
Hazelnuss
Hazelnuss
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]
pag
pag

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.
kuphryn
kuphryn
Use a temporary variable to hold the map''s key and/or value.

int nTemp = iter.second;

Kuphryn
Qw3r7yU10p!
Qw3r7yU10p!
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]
darookie
darookie
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]
darookie
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:

   
m_References.clear();

Hazelnuss
Hazelnuss
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]
Hazelnuss
Hazelnuss
Thanks, it works perfectly now.
It drove me crazy, VC++ told me it were valid members when i typed the ".".
fizban75
fizban75
Try "iter->second"
Qw3r7yU10p!
Qw3r7yU10p!
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]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.