我有一個關於性病不確定性::地圖上C++:的std ::地圖問題,關於C++
我這樣做,加載鏈接庫(的.so)C_ConfigurationLibrary
一個對象C_Configuration
。
該C_Configuration
類有一個std::map
和The C_ConfigurationLibrary
有一個方法,初始化std::map
。
如果我接觸到std::map
從C_Configuration
有 「for」 循環:
std::map<const char*, const char*>::iterator l_item;
for(l_item = m_configuration_map.begin();
l_item != m_configuration.end();
l_item++)
這是可行的;
但是,如果使用:
m_configuration[VALUE_KEY] // the value is NULL
這也不行;
我的代碼:
C_Configuration::C_Configuration()
{
m_configuration = LoadLibrary(); // load the linked library (.so)
if(m_configuration != NULL)
{
// DEBUG
LOG_DEBUG("Loading Key from plugin...");
m_configuration->LoadKeys(m_configuration_map);
std::map <const char*, const char*>::iterator l_item;
for (l_item = l_configuration_map.begin();
l_item != l_configuration_map.end();
l_item++)
{
//THIS IS OK
}
m_configuration_map[FIRST_KEY] // THIS IS NOT OK
}
}
void C_ConfigLibrary::LoadKeys(std::map<const char*, const char*>& p_configuration_map)
{
// DEBUG
LOG_DEBUG("Loading Keys...");
p_configuration_map.insert (std::make_pair<const char*, const char*>(FIRST_KEY, FIRST_VALUE));
// DEBUG
LOG_DEBUG("Loaded Key DBUS used: %s",m_dbus_used.c_str());
p_configuration_map.insert (std::make_pair<const char*, const char*>(SECOND_KEY,SECOND_VALUE));
}
你能幫助我嗎?
感謝這麼多
「這不好」是什麼意思?這是返回?它會崩潰嗎?你也有2個左邊的「太多」,這使得代碼不太直觀。 –
您正在使用'm_configuration [VALUE_KEY]',但您尚未將'VALUE_KEY'插入地圖。這是問題嗎? – Naveen
「這是不好的」意味着返回值是「NULL」,源代碼是一個示例,如果有一些錯誤爲「}」,請忽略它們,代碼編譯並運行。我認爲這是一個內存訪問問題,取決於std :: map。 – Gianfranco