Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ STL priority queue of Node Pointers

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?

like image 448
Melissa Avatar asked Feb 23 '11 00:02

Melissa


1 Answers

The class before Astar lacks the terminating ;

like image 64
Erik Avatar answered Jan 02 '23 13:01

Erik