【发布时间】:2013-11-16 19:19:35
【问题描述】:
代码示例:
#include <queue>
#include <vector>
using namespace std;
class Cell
{
public:
int totalCost = 0;
};
class Helper
{
public:
struct Comparator
{
bool operator()(Cell const *lfs, Cell const *rhs)
{
return lfs->totalCost < rhs->totalCost;
}
};
};
priority_queue<Cell*, vector<Cell*>, Helper::Comparator> path;
void function(Cell* cell)
{
cell->totalCost += rand() % 1000;
path.push(cell);
}
int main()
{
Helper help;
Cell first;
Cell second;
Cell third;
Cell fourth;
Cell fifth;
Cell* firstPtr = &first;
Cell* secondPtr = &second;
Cell* thirdPtr = &third;
Cell* fourthPtr = &fourth;
Cell* fifthPtr = &fifth;
function(firstPtr);
function(secondPtr);
function(thirdPtr);
function(fourthPtr);
function(fifthPtr);
return 0;
}
调试器截图:
我正在创建一个优先级队列,我尝试重载 () 运算符,但由于某种原因它根本不起作用(如调试器中所示)。
我感觉指针有问题,但我不知道到底是什么。
【问题讨论】:
-
你告诉它使用函子,但你从不实例化函子。
-
@ZacHowland ... 这意味着它在优先级队列的默认 ctor 中默认构造。
-
为什么你认为它不起作用?对我来说看起来像是一个有效的堆。
-
@mah 但它被使用了。队列的第三个模板参数是
Helper::Comparator,因此它由队列的默认 ctor 默认构造。 -
@mah 使用,在
path的声明中,这就是所有需要的。
标签: c++ debugging pointers operator-overloading priority-queue