-1
struct triangle {
triangle( vertice * p0,  vertice * p1,  vertice * p2)
    :tv1(0)
    ,tv2(0)
    ,tv3(0)
{
    m_Vertices[0] = p0;
    m_Vertices[1] = p1;
    m_Vertices[2] = p2;
    //SetCircumCircle();
 };  triangle *tv1,*tv2,*tv3;}


struct vertice

{    
    vertice (  int init_val=0) :id (init_val) {};
    vertice (double xx, double yy=0):x(xx),y(yy) { };
    int id;
    double x,y;
}; 
std:: vector <triangle> triangles;
 void cre_triangle()
{
    triangle r(0,1,2);
    triangle r2(3,4,6);
    r.tv1= &r2;
    r.tv2=0;
    r.tv3=0;
    cout << r.tv1->m_Vertices[0]->id << r.tv1->m_Vertices[1]->id
<< r.tv1->m_Vertices[2]->id;           //the result is  3 4 6 
    triangles.pushback(r);
} 
int main ()
{
    cre_triangle();
    cout << triangles[0].tv1->m_Vertices[0]->id <<triangles[0].tv1->m_Vertices[1]->id
      <<triangles[0].tv1->m_Vertices[2]->id << endl ;     
  // problem here  (....exe has stopped working) 
     return 0;
}  

i went to access to the ids and the coordinates of vertices of triangle r2 by r (tv1 is the neighbor 1) and modify its but i have a problem ( appilcation.exe has stopped working) please can you help me ..

AstroCB
  • 12,337
  • 20
  • 57
  • 73

1 Answers1

0

You're storing pointers to local variables. Those variables disintegrate when cre_triangle() is finished. Either use shared pointers or just store by copy.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055