c – 理解错误“在抛出’std :: length_error’的实例后调用终止
发布时间:2020-12-16 04:52:46 所属栏目:百科 来源:网络整理
导读:所以这是我的错误: terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_createAborted (core dumped) 这是我的代码: //Code removedstring generateSong(string list[],int num){ //Code removed //Code remov
所以这是我的错误:
terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_create Aborted (core dumped) 这是我的代码: //Code removed string generateSong(string list[],int num) { //Code removed //Code removed for (i = 0; i < num; i++) { output += list[i]; output += bone1; output += list[i + 1]; output += bone2; } return output; } int main() { string list[9] = { //Code removed }; //Code removed return 0; } 我只想知道这个错误意味着什么,所以我知道如何修复它.我看过很多帖子都有类似的错误,但没有完全相同.我实际上只是从C开始,到目前为止我所学到的所有答案都没有意义.如您所见,这是一个输出歌曲的简单程序.这是为了帮助我练习我正在上课的字符串,但这对我来说完全没有意义,这本书也没什么用处.有人可以向我解释一下吗? 附:如果这是有用的,它将使用g进行编译,但是当它运行时它会给出该错误(所以基本上它不是编译错误,这是一个运行错误). 解决方法
这部分代码是可疑的:
for (i = 0; i < num; i++) { output += list[i]; output += bone1; output += list[i + 1]; // <--- here output += bone2; } 您的数组长度为9,因此其中的有效索引范围为0,1,2,…,8.在迭代8中,指示的行将尝试读取数组索引9,这是无效的.这会导致未定义的行为,在您的情况下,这是一个关于无效字符串的误导性错误消息. 您必须决定要采取哪些步骤来解决这个问题,但我相信这是问题的直接原因. 希望这可以帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |