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

时间:2011-07-18 09:30:22

标签: c++ segmentation-fault

    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 ”中没有任何内容,那么它甚至不会到达内部。

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

编辑1:

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

结构是:

typedef struct branch
{
    unsigned int distance;
    int          connectedExitPoint;
} branch;

typedef struct route
{
    int    exitPoint;
    vector <branch> selectedBranchesVector;
} route;

vector <route> routeVector;

1 个答案:

答案 0 :(得分:1)

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