Customized "Compare" function for priority_queue

I have a structure "Event", for which I have defined proper comparison operators. I now want to use a priority queue in STL to save the pointers of Event, as below:

priority_queue<Event*, vector<Event*>, CompEventByPtr> m_EventQue;

The definition of the customized comparison function CompEventByPtr is:

bool CompEventByPtr(Event* pEvent1, Event* pEvent2)
{
return (*pEvent1 < *pEvent2);
}

The compiler reports the following error:
error C2923: 'std::priority_queue' : 'CompEventByPtr' is not a valid
template type argument for parameter '_Pr'

I tried another realization of CompEventByPtr:
bool CompEventByPtr(Event* pEvent1, Event* pEvent2)
{
return (pEvent1->time > pEvent2->time);
 //Not using Event's comparison operator, but directly program it here
}

The same error happens...

Thanks very much for helping me with this problem!
Last edited on
I'm sorry Ramboiory i didn't reach this level yet .I'll see anyone that could help >.<
Try making a function object:

1
2
3
4
5
struct CompEventByPtr {
    bool operator()( const Event* pEvent1, const Event* pEvent2 ) const {
        return *pEvent1 < *pEvent2;
   }
};


And now see if your declaration compiles.
Yeah, the "function object" works! Thanks very much to jsmith! :)
Topic archived. No new replies allowed.