. Advertisement .
..3..
. Advertisement .
..4..
I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at
Aborted
I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “std::out_of_range” problem, pls let me know.
Here is what I do:
struct TextLine{
//The actual text
string text;
//The line number of the document
int line_num;
//A pointer to the next line
TextLine * next;
};
Thanks!
The cause: From what you’ve mentioned, I believe the most likely issue is that insert() is being called with an invalid point off the end of the string (i.e. > size()). Because you aren’t handling the
out of range
exception (via try/catch blocks), it makes its way up to the C++ language runtime, which abruptly terminates your program.The solution: You should check the ones you’ve written to see if you’re passing position in a different way from the sample code above, and make sure the value is what you expected.
Although you’re using the std.:string::at() function somewhere in your code, it is passing an incorrect index to it. This causes it to throw. It propagates out from main(), and terminate() is called to kill the process.
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. If you are still having trouble finding the bug, please add exception handling to your code.