2011-05-05 22 views
0

崩潰我有這個代碼的問題:它總是崩潰,但當我評論:dataResults [i] .clear();崩潰後矢量只有一個清晰的

有關這個原因的想法嗎?

std::vector<std::string> r_OCRtoRetrieve; 
std::vector<std::string> DBentries; 

//stuff.. 

int distance = 9999; //TODO change here 
int minDistance = 9999; 

for(int i=0; i< r_OCRtoRetrieve.size(); i++) 
    for(int j=0; j< DBentries.size(); j++) 
    { 
     distance = calcDistance((const char *)r_OCRtoRetrieve[i].c_str(),(const char *) DBentries[j].c_str()); 

     if (distance == minDistance) 
      dataResults[i].push_back(DBentries[j]); 
     else 
      if(distance < minDistance) 
      { 
       minDistance = distance; 
       dataResults[i].clear(); 
       dataResults[i].push_back(DBentries[j]); 
      } 

    } 

編輯:

發現錯誤..我必須初始化它..這是代碼:

for(int i=0; i< r_OCRtoRetrieve.size(); i++) 
    { 
    dataResults.push_back(std::vector<std::string>()); 

    for(int j=0; j< DBentries.size(); j++) 
    { 
     distance = calcDistance((const char *)r_OCRtoRetrieve[i].c_str(),(const char *) DBentries[j].c_str()); 

     if (distance == minDistance) 
    dataResults[i].push_back(DBentries[j]); 
     else 
    if(distance < minDistance) 
    { 
     minDistance = distance; 
     if(dataResults[i].size() > 0) 
     dataResults[i].clear(); 

     dataResults[i].push_back(DBentries[j]); 
    } 
    } 

    } 
+0

你可以給我們例外信息嗎? – 2011-05-05 15:57:08

+0

什麼是你的varlable'dataResults'定義爲? – Nick 2011-05-05 16:01:51

+0

什麼是dataResults?我沒有看到聲明。 – Kevin 2011-05-05 16:02:42

回答

1

如果您收到一個指數超出範圍例外,這可能是因爲你的索引i超出了向量的範圍dataResults

如果那還不夠明顯,那麼basica如果i大於dataResults.size()那麼dataResults[i].clear();會拋出異常。

編輯:

考慮使用STL迭代器更換您的基於索引的循環,並用C++風格鑄造更換你的C風格的鑄造。你的if語句可以重新審視,以及...

編輯2:

當時一個在可能出現的問題的猜測,你沒有告訴我們什麼是例外,但你有一個vector<vector<string>>吧?如果是這樣,你需要在調用方法或構建循環之前檢查你正在索引的有效位置,以便它不會索引超出範圍。

事情是這樣:

if (dataResults.size() > i) 
{ 
    // now we know dataResults[i] will be valid 
    dataResults[i].clear(); 
    // etc 
} 

老實說,我可能會做更多的東西沿着這行:

typedef std::vector<std::string> StrArray; 
    for(StrArray::const_iterator ret(r_OCRtoRetrieve.begin()); ret != r_OCRtoRetrieve.end(); ++ret) 
    { 
     // ret will be an const iterator to each string element in r_OCRtoRetrieve 
     for(StrArray::const_iterator entry(DBentries.begin()); entry != DBentries.end(); ++entry) 
     { 
      // entry will be an const iterator to each string element in DBentries 
      distance = calcDistance(ret->c_str(), entry->c_str()); 

      // init new StrArray in dataResults as needed 
      // set new min distances as needed 
      // push back strings to dataResults 
      // whatever else you want to do 
      // yata yata 
     } 
} 
+0

多數民衆贊成在一個很好的解釋,但它沒有任何意義,因爲海報說,錯誤消失,如果該行被註釋掉。緊接着的一行還會索引dataResults [i],它會拋出非常相同的異常。 – Kevin 2011-05-05 16:06:23

+0

更準確地說,你會得到未定義的行爲 – 2011-05-05 16:07:16

+0

我要捕捉異常,因爲沒有任何消息..只是崩潰 – manty 2011-05-05 16:07:17

1

可能,你的載體dataResults的尺寸小於一定值時i

+0

不,它應該是好的 – manty 2011-05-05 16:09:52

0

啓動時DataResults中有什麼?什麼?如果不是,那麼在第一次通過時,當距離小於9999時,代碼將嘗試清除第一個零索引處不存在的位置並投訴。

如果您執行調試構建,您應該得到一個有意義的消息。

+0

當我開始我剛剛std :: vector > dataResults;呃你的解釋真的有意義...我要檢查 – manty 2011-05-05 16:09:04

+0

嘗試:\t如果(dataResults [i] .size()> 0)和相同的結果 – manty 2011-05-05 16:16:56

+0

不幸的是我現在不能這樣做。我試圖嘗試,但沒有輸出 – manty 2011-05-05 16:21:18