title: Priority Queue of pointers C++
body: <pre><code>class Edge; 
  
  class Node
  {
          public:
          Node(): distance(numeric_limits&lt;double&gt;::infinity()), visited(false) {}
          Node(string a_id): distance(numeric_limits&lt;double&gt;::infinity()), visited(false), id(a_id) {}
          bool operator==(const Node&amp; p) {return p.id == id; }
  
  
          string id;
          double distance;
          bool visited;
          string previous;
          vector&lt;Edge*&gt; edges; 
  };
  
  class Edge
  {
      public:
          Edge(double weight, string id)
          {
              this-&gt;weight = weight;
              dest = new Node(id);
          }
          double weight;
          Node * dest;
  
  };
  class Comparator {
  public:
      bool operator()(const Node* a, const Node* b)
      {
          return (a-&gt;distance &gt; b-&gt;distance);
      }
  };
  </code></pre>
  
  <p>These are my classes. I created a priority queue from vector of node pointers:</p>
  
  <pre><code>priority_queue&lt;Node*, vector&lt;Node*&gt;, Comparator &gt; queue;
  </code></pre>
  
  <p>But after some operations I get segmentation fault.</p>
  
  <pre><code>queue.push(nodes[0]);
  queue.pop();
  queue.push(nodes[1]);
  queue.push(nodes[4]);
  
  
  queue.pop();
  queue.push(nodes[3]);
  queue.push(nodes[5]);
  
  queue.pop();
  queue.push(nodes[6]);
  queue.push(nodes[7]);
  nodes[6]-&gt;distance=1;
  queue.pop();
  </code></pre>
  
  <p>At the last line      </p>
  
  <pre><code>     queue.pop();
  </code></pre>
  
  <p>I get an segmentation fault and I don't get it why it happens. Thanks in advance.</p>