stl priority queue of objects

Z

zl2k

hi, there

Can someone please tell me what's wrong with the following code? I
used to use the boost::shared_ptr in myUnit2 class and works fine, now
I take out the boost ptr and use normal pointer and got compiler
error. Please help.

-------------------in .h

class myUnit2 {
public:
double delta;
int id;
myUnit2(const double delta, const int id);
~myUnit2();

bool operator<(const myUnit2& right) const {
return delta < right.delta;
};
};

template <typename Pointer>
struct ptr_maxOnTop2{
bool operator()(const myUnit2& lhs, const myUnit2& rhs){
return lhs < rhs ;
}
};

-----------------------in .cpp

std::priority_queue<myUnit2* , std::vector< myUnit2* >, ptr_maxOnTop2<
myUnit2* > > priorityQ;

-----------------------error message
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_heap.h: In function `void
std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp,
_Compare) [with _RandomAccessIterator =
__gnu_cxx::__normal_iterator<myUnit2**, std::vector<myUnit2*,
std::allocator<myUnit2*> > >, _Distance = int, _Tp = myUnit2*,
_Compare = ptr_minOnTop2<myUnit2*>]':
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_heap.h:404: instantiated from `void
std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare)
[with _RandomAccessIterator = __gnu_cxx::__normal_iterator<myUnit2**,
std::vector<myUnit2*, std::allocator<myUnit2*> > >, _Compare =
ptr_minOnTop2<myUnit2*>]'
/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/
bits/stl_queue.h:369: instantiated from `std::priority_queue<_Tp,
_Sequence, _Compare>::priority_queue(const _Compare&, const
_Sequence&) [with _Tp = myUnit2, _Sequence = std::vector<myUnit2*,
std::allocator<myUnit2*> >, _Compare = ptr_minOnTop2<myUnit2*>]'

zl2k
 
K

Kai-Uwe Bux

zl2k said:
hi, there

Can someone please tell me what's wrong with the following code? I
used to use the boost::shared_ptr in myUnit2 class and works fine, now
I take out the boost ptr and use normal pointer and got compiler
error. Please help.

-------------------in .h

class myUnit2 {
public:
double delta;
int id;
myUnit2(const double delta, const int id);
~myUnit2();

bool operator<(const myUnit2& right) const {
return delta < right.delta;
};
};

template <typename Pointer>
struct ptr_maxOnTop2{
bool operator()(const myUnit2& lhs, const myUnit2& rhs){
return lhs < rhs ;
}

I venture the conjecture that you wanted this to be:

bool operator()( Pointer lhs, Pointer rhs){
return (*lhs) < (*rhs) ;
}
};

-----------------------in .cpp

std::priority_queue<myUnit2* , std::vector< myUnit2* >, ptr_maxOnTop2<
myUnit2* > > priorityQ;

You do not supply a comparison functor for pointers but for pointees.


[snip]

Best
Kai-Uwe Bux
 
Z

zl2k

zl2k said:
hi, there
Can someone please tell me what's wrong with the following code? I
used to use the boost::shared_ptr in myUnit2 class and works fine, now
I take out the boost ptr and use normal pointer and got compiler
error. Please help.
-------------------in .h
class myUnit2 {
public:
double delta;
int id;
myUnit2(const double delta, const int id);
~myUnit2();
bool operator<(const myUnit2& right) const {
return delta < right.delta;
};
};
template <typename Pointer>
struct ptr_maxOnTop2{
bool operator()(const myUnit2& lhs, const myUnit2& rhs){
return lhs < rhs ;
}

I venture the conjecture that you wanted this to be:

bool operator()( Pointer lhs, Pointer rhs){
return (*lhs) < (*rhs) ;
}
-----------------------in .cpp
std::priority_queue<myUnit2* , std::vector< myUnit2* >, ptr_maxOnTop2<
myUnit2* > > priorityQ;

You do not supply a comparison functor for pointers but for pointees.

[snip]

Best
Kai-Uwe Bux

Thanks, it fixed the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top