Question

My program crashes here:

void TriangleStrip::addTriangle(Triangle t){ 
   cout << t <<endl ;
   instances.push_back(t); // problem here
}

instances is:

vector<Triangle> instances;

I call addTriangle here:

TriangleStrip* s;
int c = m.getTrianglesCount();
int i;
Triangle* triangles = m.getTriangles();
for(i=0; i<c; i++){
  s->addTriangle(triangles[i]); 
}   

cout write me the triangle, but I can't put this to the vector.

What is the problem?

Was it helpful?

Solution

TriangleStrip* s; declares an uninitialized pointer, and dereferencing it s->addTriangle... is illegal.

Either initialize it with new, or don't use pointers at all - in this case you don't need pointers, just have

TriangleStrip s;

and

s.addTriangle(triangles[i]); 

OTHER TIPS

You don't ever create a TriangleStrip.

This creates a pointer to a TriangleStrip.

TriangleStrip* s;

And this expects the pointer to be assigned to a TriangleStrip. But that never happened.

s->addTriangle(triangles[i]); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top