-1

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();
}
boriaz50
  • 860
  • 4
  • 17
JKdub
  • 9
  • 2

2 Answers2

0

Before accessing the reList[0] you have to check it exists.

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();
}
boriaz50
  • 860
  • 4
  • 17
0

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.

JKdub
  • 9
  • 2