title: C++ - How to use a comparator in priority queue within a Class
body: <p>I am writing a Dijsktra program as per Cormen's book. Here, I have a class <strong>Graph</strong> which has a method <strong>dijsktra</strong>. I need to use a priority queue, <strong><em>Q</em></strong> inside this method. There are two arrays shortest[] and pred[]. This <strong><em>Q</em></strong> should sort the vertices based on shortest[] values.<br />
  Each node is represented as int.</p>
  
  <p>I am getting an error in declaring a priority Queue with a comparator.  <br />
  Any Help is much appreciated.</p>
  
  <pre><code>class Graph {
  private:
      vector&lt;int&gt; vertex;
      map&lt;int, vector&lt;int&gt; &gt; adjlist;
      int *shortest,*pred;
  public:
      Graph(int V){
          fore(i,V) { vertex.pb(i); }
          shortest = new int[V];
          pred = new int[V];
      }
      struct Comp {                     // Error!
          Comp(Graph&amp; g): graph(g){     // Error!
          }
          Graph&amp; graph;
          bool operator()(int i1, int i2) {
              return graph-&gt;shortest[i1] &gt; graph-&gt;shortest[i2];
          }
      };
      Comp comp;
      void dijsktra(int s);
   };
      void Graph::dijsktra(int s) {
          int n = vertex.size();
          fore(i,n) {
              shortest[i] = 99;
              pred[i] = 0;
          }
          shortest[s] = 0;
  
          priority_queue&lt;int,vector&lt;int&gt;, Comp&gt; Q(comp); // Error!
  
      }
  </code></pre>
  
  <p><strong>[EDIT]</strong>
  Tried various methods. I believe error is in adding a comparator<br />
  Error:</p>
  
  <pre><code>dag.cpp: In constructor ‘Graph::Graph(int)’:
  dag.cpp:15:14: error: no matching function for call to ‘Graph::Comp::Comp()’
    Graph(int V){
                ^
  dag.cpp:15:14: note: candidates are:
  dag.cpp:21:3: note: Graph::Comp::Comp(Graph&amp;)
     Comp(Graph&amp; g): graph(g){
     ^
  dag.cpp:21:3: note:   candidate expects 1 argument, 0 provided
  dag.cpp:20:9: note: constexpr Graph::Comp::Comp(const Graph::Comp&amp;)
    struct Comp {
           ^
  dag.cpp:20:9: note:   candidate expects 1 argument, 0 provided
  dag.cpp:20:9: note: constexpr Graph::Comp::Comp(Graph::Comp&amp;&amp;)
  dag.cpp:20:9: note:   candidate expects 1 argument, 0 provided
  Compilation failed.
  </code></pre>