I have a simple class
class symbol_entry
{
private:
static unsigned long uid;
public:
std::string name;
std::string filename;
unsigned int line_number;
unsigned int column_number;
symbol_entry* parent_symbol;
std::map<const char*,symbol_entry*> child_symbols;
unsigned long type_flags;
public:
symbol_entry();
symbol_entry(const char* name,
const char* filename,
int line_number,
int column_number,
symbol_entry* parent_symbol,
unsigned long type_flags);
~symbol_entry();
symbol_entry* get_child(const char* name);
bool put_child(symbol_entry* child);
};
here is the implementation of symbol_entry::put_child;
bool symbol_entry::put_child(symbol_entry* child)
{
if(child_symbols[child->name.c_str()])
return false;
child_symbols.insert(std::make_pair(child->name.c_str(),child));
return true;
}
whenever i perform a test like this;
symbol_entry* tsym=new symbol_entry("test","$",0,0,0,0);
symbol_entry* tcsym=new symbol_entry("test_child","$",0,0,0,0);
tsym->put_child(tcsym);
std::cout<<tsym->child_symbols.begin()->first<<" => "<<tsym->child_symbols.begin()->second<<std::endl;
child_symbols.begin()->second is storing a null pointer. I cannot work this out and have tried many variations including const and references with to avail.
child_symbols[child->name.c_str()]will always create and return a new map entry (a NULL one), and thenchild_symbols.insert(...)doesn’t do anything (so the value in the map stays NULL). The correct way to check whether the key is already in the map is to usefind: