简体   繁体   English

插入字符串流时出现分段错误

[英]Segmentation fault when inserting into the stringstream

I get a segmentation fault, but I don't use any pointers.我收到分段错误,但我不使用任何指针。 It's being happened when I insert into the stringstream.当我插入字符串流时,它正在发生。

std::string Relations::toString()
{
    std::stringstream restring;
    restring << ID << "(";

    restring << reList[0]; // segmentation fault

    for (int c = 1; c < reList.size(); c++)
    {
        restring << "," << reList[c];
    }

    restring << ")";
    return restring.str();
}

Before accessing the reList[0] you have to check it exists.在访问reList[0]您必须检查它是否存在。

std::string Relations::toString()
{
    std::stringstream restring;
    restring << ID << "(";

    if (reList.size() > 0)
    {
        restring << reList[0];
        for (std::size_t i = 1; i < reList.size(); ++i)
        {
            restring << "," << reList[i];
        }
    }

    restring << ")";
    return restring.str();
}

I answered my own question, making me realize that this was a dumb question.我回答了我自己的问题,让我意识到这是一个愚蠢的问题。 I was using this class wrong in another class and there was nothing going into my vector.我在另一个类中错误地使用了这个类,并且没有任何东西进入我的向量。 Therefore, I was trying to access the empty vector.因此,我试图访问空向量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM