Vector.push_back(...)读取访问冲突

时间:2018-01-02 02:35:48

标签: c++ vector

我不知道为什么但是我的程序在2个嵌入式循环的第一次迭代中在一行上触发断点就是这一行:

pointerHolder->linkedVertices.push_back(&sphereApproximation.vertices.back());

这是它所在的部分(线条靠近底部):

static const vertice holder[6] = { vertice(0,r,0,0), vertice(r,0,0,0), vertice(0,0,r,0), vertice(0,-r,0,0), vertice(-r,0,0,0), vertice(0,0,-r,0) };
std::vector<vertice> vertices (holder, holder + (sizeof(holder) / sizeof(vertice)));
shape sphereApproximation = shape(0, vertices);
int count;
for (int i = 0; i < 6; i++) {
    count = i;
    for (int t = 0; t < 5; t++) {
        if (count == 5) {
            count = 0;
        }
        else {
            count++;
        }
        if (t != 2) {
            sphereApproximation.vertices[i].linkedVertices.push_back(&sphereApproximation.vertices[count]);
        }
    }
}

bool * newConnection = new bool[pow(sphereApproximation.vertices.size(), 2) - sphereApproximation.vertices.size()]();
vertice * pointerHolder;
for (int i = 0; i < sphereApproximation.vertices.size(); i++) {
    for (int t = 0; t < sphereApproximation.vertices[i].linkedVertices.size(); t++) {
        if (!newConnection[(i * (sphereApproximation.vertices.size() - 1)) + t]) {

            pointerHolder = sphereApproximation.vertices[i].linkedVertices[t];
            sphereApproximation.vertices.push_back(newVertice(&sphereApproximation.vertices[i], pointerHolder, accuracyIterator + 1));

            for (int q = 0; q < pointerHolder->linkedVertices.size(); q++) {
                if (pointerHolder->linkedVertices[q] == &sphereApproximation.vertices[i]) {
                    pointerHolder->linkedVertices.erase(pointerHolder->linkedVertices.begin() + q);
                    break;
                }
            }

            sphereApproximation.vertices[i].linkedVertices.erase(sphereApproximation.vertices[i].linkedVertices.begin() + t);

            sphereApproximation.vertices[i].linkedVertices.push_back(&sphereApproximation.vertices.back());

            std::cout << "gets here" << std::endl;
            pointerHolder->linkedVertices.push_back(&sphereApproximation.vertices.back());
            std::cout << "does not get here" << std::endl;

            sphereApproximation.vertices.back().linkedVertices.push_back(&sphereApproximation.vertices[i]);
            sphereApproximation.vertices.back().linkedVertices.push_back(pointerHolder);
        }
    }
}

我知道newVertice(...)子例程的声明丢失,但我认为这是不必要的,所有需要知道的是它的返回类型是vertice并且它确实返回{{ 1}}正如我测试的那样。以下是我使用的结构的变化:

vertice

如果我没有提供任何内容,请发表评论,我将修改我的问题。

0 个答案:

没有答案