首页 > 解决方案 > 错误“在抛出 'std::length_error' what(): basic_string::_S_create 的实例后调用终止”

问题描述

我想从函数返回一个字符串并将该字符串添加到另一个字符串,但我偶然发现了这个错误

terminate called after throwing an instance of 'std::length_error'   what():  basic_string::_S_create

这是我的代码:

#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;

bool isTerminal(string str)
{
    if(str == "This" || str == "Computers" || str == "I" || str == "never" ||  str == "is" || str == "run" || str == "am" || str == "tell" || str =="university" || str == "world" || str == "cheese" || str == "lie")
    {
        return true;
    }
    return false;
}

string subject()
{
    string subjects[3] = {"This ", "Computers ", "I "};
    srand(time(NULL));
    int i = rand() % 3;
    cout << subjects[i];
}

string verb()
{
    string verbs[4] = {"is ","run ","am ","tell "};
    srand(time(NULL));
    int i = rand() % 4;
    return verbs[i];
}

string noun(){
    string nouns[4] = {"university ","world ","cheese ","lie "};
    srand(time(NULL));
    int i = rand() % 4;
    return nouns[i];
}

string sentence(){
    string sentence="";
    string s = subject();
    string v = verb();
    string n = noun();

    sentence = s + v + n ;
    cout << sentence << endl;
}

int main()
{
    sentence();
    return 0;
}

我基本上是编程新手,我想知道错误的含义。我查了很多网站,但似乎没有人和我有同样的问题。我想随机返回一个主语,一个动词和一个名词来组成一个句子。有人可以给我解释一下吗?

标签: string

解决方案


推荐阅读