逻辑错误:无法确定分割错误的原因

Logical error: Unable to determine the cause of segmentation fault

本文关键字:错误 分割 无法确定      更新时间:2023-10-16
    for (i = (routeVector.size () - 1); i >= 0; i--)
    {
        cout << "Connected to -> " << routeVector[i].exitPoint;
        for (j = (routeVector.size () - 1); j >= 0; j--)
        {
            if (routeVector[i].selectedBranchesVector.size() > 0)
            {
                cout << "n: routeVector[i].selectedBranchesVector[0].connectedExitPoint" <<
routeVector[i].selectedBranchesVector[0].connectedExitPoint;
                ******cout << "nrouteVector[j].exitPoint:" << routeVector[j].exitPoint;
                if (routeVector[i].selectedBranchesVector[0].connectedExitPoint == routeVector[j].exitPoint)
                {
                    cout << "Connected to -> " << routeVector[i].selectedBranchesVector[0].connectedExitPoint;
                }
            }
        }
    }

起始行给了我分割错误,我不明白为什么。

如果"routeVector"在"selectedBranchesVector"中没有任何内容,它甚至不会到达内部If。

造成上述问题的原因是什么?

编辑1:

为了使问题更清楚,我打印了语句的两个条件,错误显示在开始的行上。

结构为:

typedef struct branch
{
    unsigned int distance;
    int          connectedExitPoint;
} branch;
typedef struct route
{
    int    exitPoint;
    vector <branch> selectedBranchesVector;
} route;
vector <route> routeVector;

这取决于ij的类型-如果ijunsigned,上述循环将非常愉快地循环回来-这可能就是正在发生的事情-打印索引,您将看到…