c++ - Someone knows why there's this segmentation fault in my code? -


i'm implementing graph in c++ , i'm having segmentation fault when try print adjacency list, knows why? doing wrong? sorry bad english.

#include <iostream> #include <vector> #include <list> #include <string>  using namespace std;  class vertex;  class edge{ public:     edge(vertex *org, vertex *dest, int weight){         origem = org;         destino = dest;         peso = weight;     }      vertex* getorigem(){return origem;}     vertex* getdestino(){return destino;}     int getpeso(){return peso;}  private:     vertex* origem;     vertex* destino;     int peso; };  class vertex{ public:     vertex(string id){         name = id;     }      void addedge(vertex *v, int peso){         edge newedge(this, v, peso);         edges.push_back(newedge);     }      void printedges(){         cout << name << ":" << endl;         (int = 0; < edges.size(); i++)         {         edge e = edges[i];         cout << e.getdestino()->getname() <<             " - " << e.getpeso() << endl;         }         cout << endl;     }      string getname(){return name;}     vector<edge> getedges(){return edges;}      private:     string name;     vector<edge> edges; };  class graph{ public:     graph(){         degree = 0;     }      void insert(vertex* v){         vertices.push_back(v);         degree++;     }      int getdegree(){return degree;}      void printgraph(){         (int = 0; < vertices.size(); i++)             vertices[i]->printedges();     }      list<vertex*> adj(vertex* v){         vector<edge> edges = v->getedges();         cout << "oi\n";         list<vertex*> adj;         int nedges = edges.size();         for(int = 0; < nedges; i++)             adj.push_back(edges.at(i).getdestino());         cout << "k\n";     }      vertex* getvertex(int i){return vertices.at(i);} private:     vector<vertex*> vertices;     int degree; };   int main(){     list<list<vertex*> > listadj;     graph g;      vertex v1 = vertex("seattle");     vertex v2 = vertex("portland");     vertex v3 = vertex("everett");     vertex v4 = vertex("lynnwood");     vertex v5 = vertex("northgate");     vertex v6 = vertex("bellevue");     vertex v7 = vertex("arlington");     vertex v8 = vertex("bellingham");       vertex *vp1 = &v1;     vertex *vp2 = &v2;     vertex *vp3 = &v3;     vertex *vp4 = &v4;     vertex *vp5 = &v5;     vertex *vp6 = &v6;     vertex *vp7 = &v7;     vertex *vp8 = &v8;       v1.addedge(vp2, 100);     v1.addedge(vp6, 20);     v2.addedge(vp1, 100);     v3.addedge(vp1, 30);     v3.addedge(vp4, 10);     v3.addedge(vp7, 20);     v4.addedge(vp5, 15);     v5.addedge(vp1, 10);     v6.addedge(vp1, 20);     v8.addedge(vp7, 45);       g.insert(vp1);     g.insert(vp2);     g.insert(vp3);     g.insert(vp4);     g.insert(vp5);     g.insert(vp6);     g.insert(vp7);     g.insert(vp8);     cout << "cheguei aqui" << endl;      for(int = 0; < g.getdegree(); i++)         listadj.push_back(g.adj(g.getvertex(i)));     int k = 0;     cout << "cheguei aqui" << endl;      for(list<vertex*> i: listadj){         cout << g.getvertex(k)->getname() << ":" << endl;         list<vertex*>::const_iterator j = i.begin();         for(; j != i.end(); j++){             vertex *v = *j;             cout << v->getname() << endl;         }         k++;     }      g.printgraph();  return 1; } 

you missed return adj; in end of graph::adj().


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -