R*******y 发帖数: 19 | 1 我有一个Event类型的结构体,已经重载了比较操作符并且运行正确。为了设计上的需
要,我要写一个priority_queue,保存Event类的指针(注意不是Event类本身)。声明
如下:
priority_queue, CompEventByPtr> m_EventQue;
CompEventByPtr实现如下:
bool CompEventByPtr(Event* pEvent1, Event* pEvent2)
{
return (*pEvent1 < *pEvent2);
}
系统编译报错:
error C2923: 'std::priority_queue' : 'CompEventByPtr' is not a valid
template type argument for parameter '_Pr'
我不明白为什么这不是合法的比较函数。我将实现改为:
bool CompEventByPtr(Event* pEvent1, Event* pEvent2)
{
return (pEvent1->time > |
t****t 发帖数: 6806 | 2 you need to provide a functor (a CLASS) for this template argument, not a
function.
【在 R*******y 的大作中提到】 : 我有一个Event类型的结构体,已经重载了比较操作符并且运行正确。为了设计上的需 : 要,我要写一个priority_queue,保存Event类的指针(注意不是Event类本身)。声明 : 如下: : priority_queue, CompEventByPtr> m_EventQue; : CompEventByPtr实现如下: : bool CompEventByPtr(Event* pEvent1, Event* pEvent2) : { : return (*pEvent1 < *pEvent2); : } : 系统编译报错:
|
p***o 发帖数: 1252 | 3 Function is OK. He need to specify the type of the function in the template.
【在 t****t 的大作中提到】 : you need to provide a functor (a CLASS) for this template argument, not a : function.
|
X****r 发帖数: 3557 | 4 That could work, but --
You have to make sure you pass the actual function pointer
when creating the priority queue. Since a function pointer type
is considered primitive type and the default 'constructor' does
nothing, it is possible to create the priority queue with the
'default' compare function which would crash the application.
template.
【在 p***o 的大作中提到】 : Function is OK. He need to specify the type of the function in the template.
|
o**o 发帖数: 3964 | 5 ?
【在 X****r 的大作中提到】 : That could work, but -- : You have to make sure you pass the actual function pointer : when creating the priority queue. Since a function pointer type : is considered primitive type and the default 'constructor' does : nothing, it is possible to create the priority queue with the : 'default' compare function which would crash the application. : : template.
|
X****r 发帖数: 3557 | 6 Compiler probably won't warn you for the following definition:
priority_queue, bool (*)(Event*,Event*)>
m_EventQue;
However if you try to use m_EventQue.pop(), your code will crash.
The right definition is:
priority_queue, bool (*)(Event*,Event*)>
m_EventQue(CompEventByPtr);
【在 o**o 的大作中提到】 : ?
|