2013-03-16 131 views
0

目前,當我一直在調用我的getFunction方法時,我得到了令人討厭的崩潰。我的程序編譯得很好,但是當我運行並調用此函數時,出現「Debug Assertion Failed!」,「Expression:vector subscript out of range」。不知道如何處理這個問題,因爲我幾年來一直沒有在C++上做太多工作。C++ unorderedmap vector下標超出範圍

void* PluginMap::getFunction(char* pguid, char* fname){ 
    if(plugin_map.size()>0 && plugin_map.find(pguid)!=plugin_map.end()) 
    { 
     //plugin_map is an unorderedmap that is defined elsewhere. 
     MicroMap* mm = &plugin_map[pguid]; 
     if((*mm).find(fname)!=(*mm).end()) 
     { 
      //MicroMap is an unorderedmap that goes in plugin_map, and contains void* 
      return (*mm)[fname]; 
     } 
    } 
    return 0; 
} 

任何幫助,將不勝感激。

+0

你在使用迭代器嗎?因爲如果你是,我沒有看到你的代碼中的任何東西。 – 2013-03-16 04:06:58

+0

不,我沒有使用任何迭代器。 – robbert229 2013-03-16 04:40:14

+1

如果你使用std :: unordered_map,迭代器會讓它變得更容易。下面是一個例子:http://www.cplusplus.com/reference/unordered_map/unordered_map/find/ – 2013-03-16 04:44:51

回答

1

請避免char *與std :: unordered_map使用正確的std :: string,它應該沒問題。 char *被當作指針類型,除非你爲它定義了std :: hash,否則可能會導致一個問題。

+0

謝謝,C++對我來說有點困惑,因爲我一直在js和c#中工作,堆棧社區真棒! – robbert229 2013-03-16 04:07:53