我有一個簡單的類C++ STL地圖:刀片存儲空指針
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);
};
這裏是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;
}
每當我進行這樣的測試;
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存儲空指針。我無法解決這個問題,並嘗試了許多變體,包括const和引用來取得利用。
你將不得不發佈'put_child'的代碼。 – Puppy 2011-06-11 18:12:53
剛剛在 – jmgun87 2011-06-11 18:16:34
之上加註請注意,如果沒有將比較對象傳遞給您的'map'(請參閱http://www.sgi.com/tech/stl/Map.html上的示例),您的'const char *'鍵將會出現比較指針,而不是字符串。 – 2011-06-11 18:18:18