我有一个简单的课程
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);
};
Run Code Online (Sandbox Code Playgroud)
这是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;
}
Run Code Online (Sandbox Code Playgroud)
每当我进行这样的测试;
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;
Run Code Online (Sandbox Code Playgroud)
child_symbols.begin() - >第二个是存储空指针.我无法解决这个问题,并尝试了许多变体,包括const和引用.
child_symbols[child->name.c_str()]将始终创建并返回一个新的映射条目(一个NULL),然后child_symbols.insert(...)不执行任何操作(因此映射中的值保持为NULL).检查密钥是否已在地图中的正确方法是使用find:
if (child_symbols.find(...) != child_symbols.end()) // already exists
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2290 次 |
| 最近记录: |