简体   繁体   中英

c++ vector push_back not working

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?

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]); 

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]); 

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