473,748 Members | 4,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 DefaultHeapOrde r {
public:
template <typename T>
bool operator()(cons t T & a, const T & b)
{ return ( a < b ? true : false); }
};

template <typename T, typename F = DefaultHeapOrde r >
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 2167
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:2 1: 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
5927
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
2135
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 bsort(T * si, T * ei, FUNCTOR cmpfunc) { int k = 0; for (T * i = si; i < ei - 1; i++, k++) for (T * j = si; j < (ei-k-1) ; j++)
8
1982
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. functor<bool (long, long)> myFunctor; myFunctor = AFunctionOfThatPrototype; To get that much is elementary because the template can just store a (bool)(long,long) as a member variable in functor<T>. Here is the problem:
4
2904
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)> myFunctor = SampleFunction; string result = myFunctor(7, true); Works great thanks to the help from this group. Here's the guts so far for two-arity:
6
4996
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 for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
4
2472
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, as I expected. I haven't seen this behavior explained precisely in the .net world yet, so I wanted to check and make sure I've got it right. I apologize that this is a bit long. I've tried to keep it concise. There are code fragments here, but...
2
3911
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 an object and a unary method creates a unary_function-defived functor bound to that object and method. Code is below. // Method-to-functor adapter template<class TClass, typename TArg, typename TRet>
12
1967
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 { ... double evaluate(){
2
2288
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 ----nextSibling ---->0 | |
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.