Little problem to use a priority queue

E

eric.boissard

Hello,

I managed to implement the AStar algorithm, however I have some trouble
using the priority queue, and the 'compare' function.
Here is my 'Waypoint.h' header file:

class Waypoint
{
public:
int node_id;
float x,y,z;
float g,h,f;
int nConnections;
std::vector<int> connects;
Waypoint *parent;
bool onClosed;
bool onOpen;
};


//Comparison function used in the priority_queue
struct compare : binary_function<Waypoint *, Waypoint *, bool>
{
// Other stuff...
bool operator()(const Waypoint *x, const Waypoint *y) const {
return x->f > y->f;
}
};


My problem is as soon as I try to include 'Waypoint.h' in any .cpp
file, I have some errors:

error C2504: 'binary_function' : base class undefined
error C2143: syntax error : missing ',' before '<'

(I started to have this problem when I decided to split and reorganise
my files)

Anyone can help me please, I am just starting using stl priority
queues. I will be honest, I cut/paste this part from an example but I
really want to know how this comparison function exactly works.

Thanks a lot for your help

Eric
 
B

Ben Pope

Hello,

I managed to implement the AStar algorithm, however I have some trouble
using the priority queue, and the 'compare' function.
Here is my 'Waypoint.h' header file:

class Waypoint
{
public:
int node_id;
float x,y,z;
float g,h,f;
int nConnections;
std::vector<int> connects;
Waypoint *parent;
bool onClosed;
bool onOpen;
};


//Comparison function used in the priority_queue
struct compare : binary_function<Waypoint *, Waypoint *, bool>
{
// Other stuff...
bool operator()(const Waypoint *x, const Waypoint *y) const {
return x->f > y->f;
}
};


My problem is as soon as I try to include 'Waypoint.h' in any .cpp
file, I have some errors:

error C2504: 'binary_function' : base class undefined
error C2143: syntax error : missing ',' before '<'

(I started to have this problem when I decided to split and reorganise
my files)

Anyone can help me please, I am just starting using stl priority
queues. I will be honest, I cut/paste this part from an example but I
really want to know how this comparison function exactly works.

Perhaps you need to include the header that specifies binary_function
before using it?

#include <functional>

Perhaps you need to include the namespace std::?

struct compare : std::binary_function<Waypoint *, Waypoint *, bool>

Both, perhaps?

Ben Pope
 
M

mlimber

Hello,

I managed to implement the AStar algorithm, however I have some trouble
using the priority queue, and the 'compare' function.
Here is my 'Waypoint.h' header file:

class Waypoint
{
public:
int node_id;
float x,y,z;
float g,h,f;
int nConnections;
std::vector<int> connects;
Waypoint *parent;
bool onClosed;
bool onOpen;
};


//Comparison function used in the priority_queue
struct compare : binary_function<Waypoint *, Waypoint *, bool>
{
// Other stuff...
bool operator()(const Waypoint *x, const Waypoint *y) const {
return x->f > y->f;
}
};


My problem is as soon as I try to include 'Waypoint.h' in any .cpp
file, I have some errors:

error C2504: 'binary_function' : base class undefined
error C2143: syntax error : missing ',' before '<'

(I started to have this problem when I decided to split and reorganise
my files)

Anyone can help me please, I am just starting using stl priority
queues. I will be honest, I cut/paste this part from an example but I
really want to know how this comparison function exactly works.

Thanks a lot for your help

Eric

Looks like you just need to #include <functional> and/or qualify the
base class as std::binary_function. (You shouldn't do "using namespace
std;" in a header.) See your STL book for more on binary_function, or
see this link:

http://www.sgi.com/tech/stl/binary_function.html

Did you want to know something else about the comparison function?

Cheers! --M
 
E

eric.boissard

Thanks a lot for your help. In the code example I had 'using namespace
std', indeed I do not like using global declaration of namespaces so I
removed it...and I forgot to add the std:: prefix !!! I feel stupid ;)

Thanks for your link about binary_function, I really need to read a bit
more about that.

Thanks again for this quick answer ;)

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top