c++ - Segmentation fault in dijkstra's algorithm -


i writing c++ program code dijkstra's algorithm. here code.

#include <iostream> #include <vector> #include <map> using namespace std; class vertex; class node { public: int value; //bool exp=false; char c; }; class edge { public:     vertex* head;     vertex* tail;     int length;     edge(vertex*h,vertex* t, int l)     {         head=h;         tail=t;         length=l;     } }; class vertex:public node { public:     vector<edge*> a;     vertex& operator|(vertex &p)     {         int l;         cout<<"give length of edge "<<this->c<<p.c<<endl;         cin>>l;         edge q(&p,this,l);         a.push_back(&q);     }     vertex(char a)     {         c=a;     } }; int main() {     vertex e('e');     vertex d('d');     vertex b('b');     vertex c('c');     vertex a('a');     vertex s('s');     s.value=1;     a.value=2;     b.value=3;     c.value=4;     d.value=5;     e.value=6;     s|a;     s|b;     a|c;     b|c;         b|d;     c|d;     c|e;     d|e;     cout<<"4";     map <char ,int >a;     vector<edge*>::iterator minin;     vector<edge*>::iterator j;     int min=0;     vector<vertex*> x;     x.push_back(&s);     a['s']=0;     vector<vertex*>::iterator i=x.begin();     for(; i<x.end(); i++)     {         cout<<"1";         j=((*i)->a).begin();         for(; j<((*i)->a).end(); j++)         {             cout<<"2";             if((*j)->length+a[((*j)->tail)->c]>min)             {                 cout<<"3";                 minin=j;                 min=(*j)->length+a[((*j)->tail)->c];             }          }      }     x.push_back((*minin)->head);     a[((*minin)->tail)->c]=min;     cout<<((*minin)->head)->value;  } 

the program returns segmentation fault. have used various cout statements check fault occured nothing printed in console. however, able input edge length in console after giving input directly gives segmentation fault.

in

a.push_back(&q); 

you storing address of local object, cease exist once function terminates.


Comments