0
class vertex;
bool compareVertex(vertex *v1, vertex *v2);
std::priority_queue<vertex *, decltype(compareVertex)*> pq(compareVertex);

what's wrong with above code? i have already declare and define vertex, and compareVertex, but compiler told me: "unknown type name 'pq' " and "reference to non-static member function must be called"

thanks for your reading.

Sherwin
  • 847
  • 1
  • 6
  • 16

2 Answers2

0

You're missing a third template argument in the constructor for your priority queue.

See this post for reference.

Community
  • 1
  • 1
dBlisse
  • 801
  • 5
  • 13
  • i got it, but even if i give it a container, i still don't know how to initialize it. i tried several ways but the answer was still wrong. – Sherwin Mar 28 '14 at 06:19
0

As documented here, the signature for the std::priority_queue template is:

template<
  class T,
  class Container = std::vector<T>,
  class Compare = std::less<typename Container::value_type>
> class priority_queue;

So you need to specify the underlying container as a template argument. You can't rely on the default template argument, as you're trying to specify the comparator (third template argument). Also, I'm not sure that the way you specify your comparator is correct... You might want to specialize std::less for vertex to use your custom comparison function instead, and rely on the default template parameters.
You may want to try this:

class vertex;
bool compareVertex(vertex *v1, vertex *v2);
namespace std {
  template<>
  struct less <vertex*> {
    bool operator() (vertex* lhs, vertex* rhs) const {
      return compareVertex(lhs, rhs);
    }
  };
}
std::priority_queue<vertex*> pq;

Note that your less specialization has to be defined inside the global or std namespace.

EDIT:
Apparently, from the comments, your compareVertex function is actually a member function of some graph class.
In that case, you probably want to do something like this:

struct VertexComparator {
  explicit VertexComparator(graph& g)
  : graph_(g) {
  }
  bool operator() (vertex* lhs, vertex* rhs) const {
    return graph_.compareVertex(lhs, rhs);
  }
  private:
  graph& graph_;
};

typedef priority_queue<vertex*, vector<vertex*>, VertexComparator> tVertexPriorityQueue;

graph aGraphInstance;
tVertexPriorityQueue pq(VertexComparator(aGraphInstance));
Martin J.
  • 5,028
  • 4
  • 24
  • 41
  • bool graph::compareVal(vertex *v1, vertex *v2) { return v1->distdist; } – Sherwin Mar 28 '14 at 06:06
  • above is my comparaVertex(name is different, my comparaVertex is a member function of graph), i try the way you told me ,but their are still some problem, when i wrote the 'const', the compiler told me "expected ';' at the end of declaration list" and "operator() cannot be the name of a variable or data member." then i removed the 'const', the compiler told me "call to non-static member function without an object argument" at the sentence "return graph::comparaVertex(v1,v2);" i'm confused with all these wrong, could you help me make it out? – Sherwin Mar 28 '14 at 06:15
  • That was a lot of stupid syntax errors on my parts. They are fixed now, sorry about this. – Martin J. Mar 28 '14 at 06:29
  • the last problem still exists: "call to non-static member function without an object argument" my compareVertex is a member function of graph, so i write the return in this way "return graph::compareVertex(lhs,rhs);" what's wrong here??? and when i make compareVertex a normal function, nothing wrong. – Sherwin Mar 28 '14 at 07:38
  • As the diagnostic implies, you can't call a member function of your graph object without having an object to call it on. I solved the problem you posted in your question, which said nothing about the compare function being a member function of some class. Please edit que question if it doesn't match your actual problem. – Martin J. Mar 28 '14 at 07:41