C++ std::out_of_range 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2918267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:29:33  来源:igfitidea点击:

std::out_of_range error?

c++stringlinked-listoutofrangeexception

提问by vette982

I'm dealing with a file with a linked list of lines with each node looking like this:

我正在处理一个带有行链接列表的文件,每个节点看起来像这样:

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

and I'm writing a function that adds spaces at the beginning of the lines found in the variable text, by calling functions like linelist_ptr->text.insert(0,1,'\t');

我正在编写一个函数text,通过调用类似的函数,在变量中找到的行的开头添加空格linelist_ptr->text.insert(0,1,'\t');

The program compiles, but when I run it I get this error:

该程序编译,但当我运行它时,我收到此错误:

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

Any ideas?

有任何想法吗?

回答by CMircea

You are using the std::string::at() somewhere in your code but you are passing it an incorrect index, hence it throws. Since you don't catch any exceptions it propagates out of main() and terminate() is called, killing the process.

您在代码中的某处使用 std::string::at() 但传递给它的索引不正确,因此它会抛出。由于您没有捕获任何异常,因此它会从 main() 传播出去并调用 terminate(),从而终止进程。

The code you've shown can't fail in that way, as std::string::insert() doesn't call std::string::at(), nor are the parameters. Please add exception handling to your code and if you still can't find the bug please post a larger section of your code (or the whole file, to http://codepad.orgpreferably).

您展示的代码不会以这种方式失败,因为 std::string::insert() 不调用 std::string::at(),参数也不调用。请在您的代码中添加异常处理,如果您仍然无法找到错误,请发布更大的代码部分(或整个文件,最好是http://codepad.org)。

回答by Owen S.

I think the most likely problem from what you've described is that insert() is being invoked with an invalid position off the end of the string (i.e. > size()). You say this example is likethe 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.

我认为你所描述的最有可能的问题是 insert() 是在字符串末尾的无效位置(即 > size())下调用的。你说这个例子就像你正在调用的函数,所以检查你可能写的那些你可能与上面的示例代码不同的传递位置的地方,并确保值是你期望的。

The terminate message is because you're not handling the out_of_rangeexception (via try/catch blocks), so it escapes up to the C++ language runtime, which unceremoniously shuts your program down.

终止消息是因为您没有处理out_of_range异常(通过 try/catch 块),所以它转义到 C++ 语言运行时,这会毫不客气地关闭您的程序。

回答by hamishmcn

Check that linelist_ptrhas a legitimate value, i.e. that you have newed it (and it hasn't been deleted prior to you using it)

检查它linelist_ptr是否具有合法值,即您是否已new编辑它(并且delete在您使用它之前尚未对其进行编辑)