简体   繁体   中英

“list iterator not dereferencable” on creation of object

I'm creating a computer graphics project using C++ and QT in visual studio.

The line I'm getting this error on is

quad *bodyQuad = &quad(bodyTri1, bodyTri2);

in the following method:

void MyGLWidget::createCharacter1(tree *t, node *root) {
    triangle bodyTri1 = triangle(vec3(4, 0, 0), vec3(-1, 1, 0));
    triangle bodyTri2 = triangle(vec3(-6, 0, 0), vec3(-1, -1, 0));
    bodyTri2 = *bodyTri2.applyTransform(mat3::translation2D(5, 1));
    quad *bodyQuad = &quad(bodyTri1, bodyTri2);
    root = new node(bodyQuad, 2);
}

triangle and quad are both derived from a virtual shape class. The shape class has a std::list in it for storing vertices.

node also has an std::list for storing its children.

In the constructor of both triangle and quad the vertices are added to the list

triangle's constructor

triangle::triangle(const vec3& vector1, const vec3& vector2) {
    numTriangles = 1;
    isOneColor = true;
    vec3 origin = vec3(0, 0, 1);// '1' to signify a point
    data = mat3(vector1, vector2, origin);
    data = data.transpose();

    //push "origin" vertex
    vertices.push_back(data[2]);
    //make sure point/vector convention followed, and push "base" vertex
    vertices.push_back(data[0] + vec3(0, 0, 1));
    //make sure point/vector convention followed, and push "height" vertex
    vertices.push_back(data[1] + vec3(0, 0, 1));

}

quad's constructor

quad::quad(triangle triangle1, triangle triangle2) {

    numTriangles = 2;
    isOneColor = true;

    data[0] = triangle1;
    data[1] = triangle2;

    std::list<vec3> tri1Vertices = data[0].getVertices();
    std::list<vec3> tri2Vertices = data[1].getVertices();

    for (size_t i = 0; i < tri1Vertices.size(); i++) {
        vertices.push_back(tri1Vertices.front());
        tri1Vertices.pop_front();
    }

    for (size_t i = 0; i < tri2Vertices.size(); i++) {
        vertices.push_back(tri2Vertices.front());
        tri2Vertices.pop_front();
    }
}

My problem was that my applyTransform() was returning a shape pointer, but in my return statement I was simply dereferencing & a temp variable used in applyTransform() instead of using the new keyword.

Thank you to everyone who helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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