簡體   English   中英

std :: out_of_range錯誤?

[英]std::out_of_range error?

我正在處理一個帶有鏈接的鏈表的文件,每個節點看起來像這樣:

struct TextLine{
    //The actual text
    string text;
    //The line number of the document
    int line_num;
    //A pointer to the next line
    TextLine * next;
};

並且我正在編寫一個函數,該函數通過調用諸如linelist_ptr->text.insert(0,1,'\\t');函數,在變量text找到的行的開頭添加空格linelist_ptr->text.insert(0,1,'\\t');

該程序可以編譯,但是當我運行它時,出現此錯誤:

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at
Aborted

有任何想法嗎?

您在代碼中的某處使用了std :: string :: at(),但是傳遞給它的索引不正確,因此拋出該錯誤。 由於您沒有捕獲任何異常,因此它會從main()中傳播出去,並調用終止(),從而終止進程。

您顯示的代碼不會以這種方式失敗,因為std :: string :: insert()不會調用std :: string :: at(),參數也不會。 請在代碼中添加異常處理,如果仍然找不到錯誤,請在代碼的較大部分(或整個文件,最好放到http://codepad.org )上發布。

我認為您所描述的最可能出現的問題是,在字符串末尾的無效位置(即> size())調用了insert()。 the functions you're calling, so check the ones you may have written where you might be passing position differently than the sample code above and make sure the value is what you expect. 您說這個示例您要調用的函數,因此請檢查您可能編寫的傳遞位置的代碼與上面的示例代碼不同,並確保該值是您期望的值。

終止消息是因為您沒有處理out_of_range異常(通過try / catch塊),所以它逃逸到了C ++語言運行時,從而無意中關閉了程序。

檢查linelist_ptr是否具有合法值,即是否已對其進行了new編輯(並且在使用前未將其delete

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM