C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help Please: Passing Functor Object with Template type param

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
CoolPint
Guest





PostPosted: Tue Dec 23, 2003 10:28 am    Post subject: Help Please: Passing Functor Object with Template type param Reply with quote



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 (*)())'

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 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);
};
Back to top
tom_usenet
Guest





PostPosted: Tue Dec 23, 2003 11:48 am    Post subject: Re: Help Please: Passing Functor Object with Template type p Reply with quote



On 23 Dec 2003 02:28:51 -0800, [email]coolpint (AT) yahoo (DOT) co.uk[/email] (CoolPint) wrote:

Quote:
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
//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

Back to top
CoolPint
Guest





PostPosted: Wed Dec 24, 2003 5:06 am    Post subject: Re: Help Please: Passing Functor Object with Template type p Reply with quote



Quote:
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

That's why! Thanks! How silly can I get? Now you mention it, it indeed
looks
like a function prototype...

Quote:
//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?

Quote:
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.

Back to top
tom_usenet
Guest





PostPosted: Mon Dec 29, 2003 3:25 pm    Post subject: Re: Help Please: Passing Functor Object with Template type p Reply with quote

On 23 Dec 2003 21:06:53 -0800, [email]coolpint (AT) yahoo (DOT) co.uk[/email] (CoolPint) wrote:

Quote:
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
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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.