理解错误"在抛出'std :: length_error'的实例后调用终止what():basic_string :: _ S_create Aborted(core dumped)"

scb*_*ham 11 c++ string runtime-error

所以这是我的错误:

terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_S_create
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

//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;
}
Run Code Online (Sandbox Code Playgroud)

我只想知道那个错误意味着什么,所以我知道如何修复它.我看过很多帖子都有类似的错误,但没有完全相同.我实际上刚开始用C++开始,到目前为止我所学到的答案都没有.如您所见,这是一个输出歌曲的简单程序.这是为了帮助我练习我正在上课的弦乐,但这对我来说完全没有意义,这本书也没什么用.有人可以向我解释一下吗?

PS如果这很有用,它将使用g ++进行编译,但是当它运行时它会产生错误(所以基本上它不是编译错误,而是运行错误).

tem*_*def 14

这部分代码是可疑的:

 for (i = 0; i < num; i++)
 {
    output += list[i];
    output += bone1;
    output += list[i + 1]; // <--- here
    output += bone2;
 }
Run Code Online (Sandbox Code Playgroud)

您的数组长度为9,因此其中的有效索引范围为0,1,2,...,8.在迭代8中,指示的行将尝试读取数组索引9,这是无效的.这会导致未定义的行为,在您的情况下,这是一个关于无效字符串的误导性错误消息.

您必须决定要采取哪些步骤来解决这个问题,但我相信这是问题的直接原因.

希望这可以帮助!