Passing a function_pointer as a comparator in a stl make_heap c++ -
i'm developing program run dijkstra's algorithm heap implementation , want versatile can i'm using function pointer in order avoid code repetition. error pops. i'm using stl make_heap
"type must use '.*' or '->*' call pointer-to-member function in '__comp (...)', e.g. '(... ->* __comp) (...)' "heap.h c/c++ problem
here dijkstra's algorithm:
void graph::dijkstraalg(vertex* ini, vertex* fin, void(graph::*weight_filler)(void), bool(graph::*pq_order)(const vertex* &, const vertex* &)){ for(unsigned int = 0; < vertexs.size(); i++) { vertexs[i]->path = null; vertexs[i]->holder = max_int_value; vertexs[i]->processing=false; } (this->*weight_filler)(); vertex* v=ini; v->holder = 0; v->processing = true; vector<vertex*> pq; pq.push_back(v); make_heap(pq.begin(),pq.end(),pq_order); while(!pq.empty()){ v=pq.front(); pop_heap(pq.begin(),pq.end()); pq.pop_back(); for(unsigned int u=0; u < v->adj.size(); u++){ vertex* w = v->adj[u]->dest; if((v->holder+v->adj[u]->weight) < w->holder){ w->holder=v->holder + v->adj[u]->weight; w->path=v; if(!w->processing){ w->processing=true; pq.push_back(w); } } make_heap(pq.begin(),pq.end(),pq_order); } } return;}
the error in make_heap , can't figure out, appreciated.
here function i'm passing make_heap:
bool graph::regular_pqorder(const vertex* &v, const vertex* &u){ return v->holder > u->holder;}
this how call algo:
dijkstraalg(i,f,&graph::price_weightfiller,&graph::regular_pqorder);
if need more information tell me , edit it. thank pal's
i have simplified problem to:
#include <algorithm> #include <vector> using namespace std; class vertex {}; class graph { public: void dijkstraalg(vertex* ini, vertex* fin, void(graph::*weight_filler)(void), bool(graph::*pq_order)(const vertex*, const vertex*)) { (this->*weight_filler)(); struct comparator { private: graph* m_g; bool(graph::*m_fn)(const vertex*, const vertex*); public: comparator(graph* g, bool(graph::*fn)(const vertex*, const vertex*)) : m_g(g), m_fn(fn) {} bool operator()(const vertex* one, const vertex* two) const { return (m_g->*m_fn)(one, two); } }; comparator comparator(this, pq_order); vector<vertex*> pq; std::make_heap(pq.begin(), pq.end(), comparator); } void weight_filler1() { } bool pq_order1(const vertex*, const vertex*) { return false; } }; int main() { graph g; g.dijkstraalg(nullptr, nullptr, &graph::weight_filler1, &graph::pq_order1); return 0; }
the problem is: std::make_heap
expects function pointer- in case passing pointer member function issue.
either can change declaration of dijkstraalg
take in static function pointer pq_order
or wrap pq_order
in structure make callable entity in have done.
note: have fixup references pointers in pq_order
make_heap
compile anyway
Comments
Post a Comment