What's wrong with this code? It seems to work fine most of the time, but I'm getting EXC_BAD_ACCESS on some occasions.
Here is the debug stack:
Code:
#4 0x00018016 in std::map<std::string, Item*, std::less<std::string>, std::allocator<std::pair<std::string const, Item*> > >::find at stl_map.h:542
#3 0x00017f51 in std::_Rb_tree<std::string, std::pair<std::string const, Item*>, std::_Select1st<std::pair<std::string const, Item*> >, std::less<std::string>, std::allocator<std::pair<std::string const, Item*> > >::find at stl_tree.h:1378
#2 0x00017efe in std::less<std::string>::operator() at stl_function.h:227
#1 0x00002fff in std::operator< <char, std::char_traits<char>, std::allocator<char> > at basic_string.h:2228
#0 0x97dab336 in std::string::compare
Why would this crash on string compare? I've tried several variations on the format of the string being passed to find(), but can't get any of them to work.
Thanks so much for your help!
Code:
// Group.h
#include "Item.h"
typedef map<string, Item*> GroupMap;
class Group {
public:
Group();
void addItem(string name, Item* item);
Item* getItem(string name);
protected:
GroupMap items;
};
// Group.cpp
#include "Group.h"
Group::Group() {}
void Group::addItem(string name, Item* item) {
items.insert(pair<string,Item*>(name.data(),item));
}
Item* Group::getItem(string name){
GroupMap::iterator it;
it=items.find(name.data());
if(it != items.end()) {
return (*it).second;
}
return NULL;
}