473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Help Please: Passing Functor Object with Template type parameter

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);
};
Jul 22 '05 #1
3 2137
On 23 Dec 2003 02:28:51 -0800, co******@yahoo.co.uk (CoolPint) wrote:
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


That's a function declaration! (a function called pq4 taking a Functor
and returning a PQueue<int, Functor>). I think you meant:

//extra parentheses prevent parsing as function decl
PQueue<int, Functor> pq4((Functor()));

or
// named object also works.
Functor f;
PQueue<int, Functor> pq4(f);

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #2
> >PQueue<int, Functor> pq4(Functor()); <------- This causes the error

That's a function declaration! (a function called pq4 taking a Functor
and returning a PQueue<int, Functor>). I think you meant:
That's why! Thanks! How silly can I get? Now you mention it, it indeed
looks
like a function prototype...
//extra parentheses prevent parsing as function decl
PQueue<int, Functor> pq4((Functor()));
I am afraid this still gives me an error. g++ says

pqdriver.cpp:21: parse error before `)' token

I again feel stupid not to be able to see what makes the compiler
complain. Would you kindly help me out one more time?
or
// named object also works.
Functor f;
PQueue<int, Functor> pq4(f);


Yes, this works and of course it should....

Thank you, Tom... I think you helped me last time too. This group is
turning out to be a very valuable place for learning experience.
Thanks.
Jul 22 '05 #3
On 23 Dec 2003 21:06:53 -0800, co******@yahoo.co.uk (CoolPint) wrote:
>PQueue<int, Functor> pq4(Functor()); <------- This causes the error


That's a function declaration! (a function called pq4 taking a Functor
and returning a PQueue<int, Functor>). I think you meant:


That's why! Thanks! How silly can I get? Now you mention it, it indeed
looks
like a function prototype...
//extra parentheses prevent parsing as function decl
PQueue<int, Functor> pq4((Functor()));


I am afraid this still gives me an error. g++ says

pqdriver.cpp:21: parse error before `)' token

I again feel stupid not to be able to see what makes the compiler
complain. Would you kindly help me out one more time?


(Delayed by xmas!)

That's a gcc bug. I reported it a while back, and I think it is marked
as closed for gcc 3.4 (due soon?), which includes a brand new
hand-coded parser.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gert Van den Eynde | last post by:
Hi all, I'm struggling a bit with Functors generated for an ABC. This is the functor code: class Functor{ public: virtual double operator(double x)=0 }
3
by: CoolPint | last post by:
Can anyone explain how I can make the following function accept an default arguement for the last parameter, which should be an optional functor? template <typename T, typename FUNCTOR> void...
8
by: daniel.w.gelder | last post by:
Hello, I have been trying to write a functor template for a week now and I'm just having tons of trouble because I don't understand an issue that I guess is pretty basic to this task. ...
4
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)>...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
2
by: zzorozz | last post by:
I often find that I need to pass a non-static method as a call-back instead of a functor or a function. The problem with that is the implicit 'this' parameter. So I created an adaptor that given...
12
by: aaragon | last post by:
Hi everyone, I'm trying to provide some external functionality to a class through a functor object defined by the user. The concept is as follows: template <class Functor> class ClassA {...
2
by: aaragon | last post by:
Hi guys, Is there a way to return a functor from a recursive call that takes different paths? Let's say that I have a tree structure like: root | first child ---- nextSibling ----nextSibling...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.