C++ STL节点指针优先级队列

发布于 2024-10-18 21:27:04 字数 1259 浏览 4 评论 0原文

我正在尝试实现 Node* 的优先级队列,其中 Node 是我自己定义的类。我意识到拥有一个指针优先队列意味着它将根据地址而不是节点所持有的值进行排序,因此我搜索了相当多的讨论论坛以找到一个解决方案,该解决方案允许我指定如何对优先级队列中的Node对象进行排序;大多数人都同意您需要编写一个结构体,其中包含一个函数,该函数接受 2 个 Node 对象作为参数并返回所需的比较。以下是我的 Node 类(缩写)和我编写的用于比较 2 个 Node 对象的结构,它们位于同一头文件中:

class Node {

public:

  ...

  int fValue() const { cerr << fValue() << endl; return c + h; };

  ...

private:
  ...
  int c;
  int h;

  ...
};                                                                                             

struct CompareNode : public std::binary_function<Node*, Node*, bool>                                                                                     
{
  bool operator()(const Node* lhs, const Node* rhs) const
  {
    return lhs->fValue() < rhs->fValue();
  }
}

我将优先级队列构造为另一个类的成员,位于不同的头文件中,其中包含以下头文件:包含上述定义。该类缩写如下:

class Astar {

public:

  ...

private:

  ...
  priority_queue<Node*, vector<Node*>, CompareNode> frontier;
};

当我尝试编译时,出现此错误:

astar.h:28: error: multiple types in one statements make: * [astar.o] 错误 1

,其中 astar.h 的第 28 行对应于 Astar 类的末尾 (};)。

由于这是大多数论坛上提供的解决方案,我不明白这里发生了什么。有人对我有任何见解吗?

I'm trying to implement a priority queue of Node*, where Node is a class I have defined myself. I realized that having a priority queue of pointers would mean that it would sort based on the address, rather than the value that the Node held, and so I searched through quite a few discussion forums to find a solution that would allow me to specify how to sort the Node objects in the priority queue; most agree that you need to write a struct that contains a function that takes as arguments 2 Node objects and returns the desired comparison. The following is my Node class (abbreviated) and the struct I wrote to compare 2 Node objects, which are in the same header file:

class Node {

public:

  ...

  int fValue() const { cerr << fValue() << endl; return c + h; };

  ...

private:
  ...
  int c;
  int h;

  ...
};                                                                                             

struct CompareNode : public std::binary_function<Node*, Node*, bool>                                                                                     
{
  bool operator()(const Node* lhs, const Node* rhs) const
  {
    return lhs->fValue() < rhs->fValue();
  }
}

I construct the priority queue as a member of another class in a different header file that includes the header file that contains the above definitions. This class is abbreviated as follows:

class Astar {

public:

  ...

private:

  ...
  priority_queue<Node*, vector<Node*>, CompareNode> frontier;
};

When I try to compile, I get this error:

astar.h:28: error: multiple types in one declaration
make: * [astar.o] Error 1

where line 28 of astar.h corresponds to the end of the Astar class (};).

Since this is the solution provided on most forums, I don't understand what's going on here. Does anyone have any insight for me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤单情人 2024-10-25 21:27:04

Astar之前的类缺少终止符;

The class before Astar lacks the terminating ;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文