473,385 Members | 1,707 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,385 software developers and data experts.

c++ callback functor question

Hi,
I am trying to use C++ functor as callback implementation and I write
the following code to some simple test.

#include <iostream>
using std::cout;

class N0
{
public:
virtual void operator() = 0; // Line 7
};

class Negate : public N0
{
public:
virtual void operator() ( ) { cout << "int:" ;}
};

class Negate2 : public Negate
{
public:
virtual void operator() () { cout << "float:" ;}
};

int main()
{
Negate N1;
Negate2 N2;
N0* table[] = { &N1, &N2};
(*table[0])(); // Line 39
(*table[1])(); // Line 40
}
My compiler is gcc 3.2.3 and it gives me the following error message,

main.c:7: declaration of `operator()' as non-function
main.c: In function `int main()':
main.c:39: no match for call to `(N0) ()'
main.c:40: no match for call to `(N0) ()'
I wonder what is wrong here?

Apr 5 '06 #1
3 4283
* li*****@hotmail.com:
virtual void operator() = 0; // Line 7


arguments
Apr 5 '06 #2
Arent you missing a ( ) in the declaration of the virtual function in
class N0 ?

Apr 5 '06 #3
li*****@hotmail.com wrote:
Hi,
I am trying to use C++ functor as callback implementation and I write
the following code to some simple test.

#include <iostream>
using std::cout;

class N0
{
public:
virtual void operator() = 0; // Line 7
};

class Negate : public N0
{
public:
virtual void operator() ( ) { cout << "int:" ;}
};

class Negate2 : public Negate
{
public:
virtual void operator() () { cout << "float:" ;}
};

int main()
{
Negate N1;
Negate2 N2;
N0* table[] = { &N1, &N2};
(*table[0])(); // Line 39
(*table[1])(); // Line 40
}
My compiler is gcc 3.2.3 and it gives me the following error message,

main.c:7: declaration of `operator()' as non-function
A function needs an argument list in parens. The () after 'operator' are
just part of the operator's name and not the argument list. So it must be:

virtual void operator()() = 0; // Line 7

You did it correct in Negate and Negate2.
main.c: In function `int main()':
main.c:39: no match for call to `(N0) ()'
main.c:40: no match for call to `(N0) ()'


Those probably are an aftereffect of the first error.

Apr 5 '06 #4

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

Similar topics

2
by: Chris Morley | last post by:
Hi, I have always done my C++ class callbacks with the age old 'using this pointer in parameter of the class's static callback function' and typecasting it to get the specific instance. However...
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)>...
7
by: Steven T. Hatton | last post by:
Can someone provide a fromal definition of "callback"? I see this term used often, and can usually understand the discussion where it's used, but I would be hard-pressed to provide a formal...
4
by: Agoston Bejo | last post by:
Hi, I would like to do something like this: struct A { int f(float f); }; .... int g(int(*f1)(float)) { return f1(6.5); }
12
by: Darwin Lalo | last post by:
I have a lot of code like this: VOID CALLBACK TimerRoutine(PVOID lpParam) { long nTaskid = (long)lpParam; GObj *obj; if( mapThreadSafe.find( nTaskid, obj )) // mapThreadSafe is a hash_map,...
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: Lionel B | last post by:
I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1...
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.