0

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?

Tom
  • 1,027
  • 2
  • 16
  • 35

2 Answers2

3

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]); 
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

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]); 
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180