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

ptr_map help!

Started by Waffler May 1, 2007 at 4:25 PM 6 replies 3.3k views
Original Post
Waffler
Waffler
I have been racking my brains for most of the day trying to figure out how to use boost::ptr_map. I looked around for information, but nothing really answered my questions. Can somebody please help? I want to be able to: * Add objects to a ptr_map. * Use std::string as the key. * Acquire the pointer to the object given a key string. I know these are pretty basic things, but I haven't been able to get it to work. Even though I know the key exists, when this function is called it doesn't work. It just returns NULL.

Obj *get_pointer(const char *name)
{
  boost::ptr_map<std::string, Obj>::iterator iter;
  std::string tmp_name = name;

  iter = my_ptr_map.find(tmp_name);
  if (iter != my_ptr_map.end())
  {
    // I'm not sure about the following line
    return (Obj*)&iter
  }
  return NULL;
}


ldeej
ldeej
Doesn't &(*itr) work?
Antheus
Antheus
iter->second, iter.second or (*iter).second?

I'm not really sure how exactly these iterators are set up, I know they do some redirection.
Waffler
Waffler
I think the problem is somehow not being able to find the key correctly ... or something. In my debugger I can see that my_ptr_map does have a key and pointer stored in it, and I think iter has also found it ... I may be wrong. Something isn't working.
Waffler
Waffler
I found that If I change the line
iter = my_ptr_map.find(tmp_name);

to
iter = my_ptr_map.begin();

it works.

How do I get find to work correctly?
ldeej
ldeej
Are you sure the strings are the same size and in the same case?

I would try iterating over the map manually and comparing the strings to make sure the data is what you think it is.
Waffler
Waffler
Never mind. I feel stupid. I misspelled the key string I was using! I can't believe I missed that.
Agony
Agony
Note that there is no need for the variable tmp_name. Any function that accepts a (const std::string&) or a (std::string) argument will automatically convert a (const char*) into a temporary string object. You can simply call .find(name).
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o

Topic Locked

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

Sign in to reply to this topic.