C
CoolPint
I have implemented a generic priority queue below and tested it works
fine, but I have one small problem I cannot understand. I have type
parameter F which determines the priority so that users can
instantiate in the following ways
PQueue<int> pq1;
PQueue<int, Functor> pq2; // where Functor is a name of user-defined
class
I also added another constructor to accept a function pointer so that
users can use normal functions as callback too. For example,
PQueue<int, bool (*) (int, int)> pq3(function);
// where function is a user-defined function which accepts ints and a
bool
So far so good. I then got a bit curious and issued something like
this:
PQueue<int, Functor> pq4(Functor()); <------- This causes the error
thinking this would assign Functor class to F and then assign an
object of Functor class to cmp, but it got me a compiler error which I
cannot understand.
I am interested to know what the error message means and what is
causing the problem. Can somebody kindly explain what is happening?
The compiler error I got when I tried to compile above with g++ is
something like below:
pqdriver.cpp:23: request for member `enQueue' in `numq(isGreater
(*)())', which
is of non-aggregate type `PQueue<int, isGreater> ()(isGreater
(*)())'
I would very much appreciate a kind explanation as to what the problem
is what is causing it. And maybe a solution,, but I am more interested
in knowing what's happening so that I can learn from it. Thanks a lot
in advance!
class DefaultHeapOrder {
public:
template <typename T>
bool operator()(const T & a, const T & b)
{ return ( a < b ? true : false); }
};
template <typename T, typename F = DefaultHeapOrder >
class PQueue {
public:
PQueue();
explicit PQueue(F);
template <typename Iterator>
PQueue(Iterator, Iterator);
bool isEmpty() const;
bool isFull() const;
bool enQueue(const T &);
bool deQueue(T &);
bool getTop(T &);
void clear();
private:
int counter;
DArray<T> heap;
F cmp;
void buildHeap();
void shiftdown(int);
};
fine, but I have one small problem I cannot understand. I have type
parameter F which determines the priority so that users can
instantiate in the following ways
PQueue<int> pq1;
PQueue<int, Functor> pq2; // where Functor is a name of user-defined
class
I also added another constructor to accept a function pointer so that
users can use normal functions as callback too. For example,
PQueue<int, bool (*) (int, int)> pq3(function);
// where function is a user-defined function which accepts ints and a
bool
So far so good. I then got a bit curious and issued something like
this:
PQueue<int, Functor> pq4(Functor()); <------- This causes the error
thinking this would assign Functor class to F and then assign an
object of Functor class to cmp, but it got me a compiler error which I
cannot understand.
I am interested to know what the error message means and what is
causing the problem. Can somebody kindly explain what is happening?
The compiler error I got when I tried to compile above with g++ is
something like below:
pqdriver.cpp:23: request for member `enQueue' in `numq(isGreater
(*)())', which
is of non-aggregate type `PQueue<int, isGreater> ()(isGreater
(*)())'
I would very much appreciate a kind explanation as to what the problem
is what is causing it. And maybe a solution,, but I am more interested
in knowing what's happening so that I can learn from it. Thanks a lot
in advance!
class DefaultHeapOrder {
public:
template <typename T>
bool operator()(const T & a, const T & b)
{ return ( a < b ? true : false); }
};
template <typename T, typename F = DefaultHeapOrder >
class PQueue {
public:
PQueue();
explicit PQueue(F);
template <typename Iterator>
PQueue(Iterator, Iterator);
bool isEmpty() const;
bool isFull() const;
bool enQueue(const T &);
bool deQueue(T &);
bool getTop(T &);
void clear();
private:
int counter;
DArray<T> heap;
F cmp;
void buildHeap();
void shiftdown(int);
};