0

我尝试从 vector3f 访问/打印数据,结果是EXC_BAD_ACCESS

std::cout << myMesh.faces[1].vertices[1].pos.x;

代码:

struct Vector2f{
    float x, y;
};
struct Vector3f{
    float x, y, z;
};

struct ObjMeshVertex{
    Vector3f pos;
    Vector2f texcoord;
    Vector3f normal;
};

struct ObjMeshFace{
    ObjMeshVertex vertices[3];
    ObjMeshFace(){}
    ObjMeshFace(const ObjMeshFace& o)
    {for (int i=0; i < 3; ++i) vertices[i] = o.vertices[i]; }
};

struct ObjMesh{
    std::vector<ObjMeshFace> faces;
};

ObjMesh myMesh;

for(size_t i = 0; i < faces.size(); ++i){
    ObjMeshFace face;
    for(size_t j = 0; j < 3; ++j){
        face.vertices[j].pos        = positions[(faces[i].pos_index[j] - 1)];
        face.vertices[j].texcoord   = texcoords[faces[i].tex_index[j] - 1];
        face.vertices[j].normal     = normals[faces[i].nor_index[j] - 1];
    }
    myMesh.faces.push_back(face);
}

调试器指的是stl_vector.h

  reference
  operator[](size_type __n)
  { return *(this->_M_impl._M_start + __n); }

这是什么意思?我打电话超出范围了吗?

4

1 回答 1

0

已经有一段时间了,但是当您执行 push_back 时,您只是浅复制了 vertices 数组vertices[] = vertices[]。您需要 ObjMeshFace 中的自定义 copy-ctor。这就是为什么vertices[0].pos.x有效而vertices[1].pos.x无效的原因。

于 2012-05-30T18:11:51.453 回答