c++ callback functor question

L

linq936

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?
 
N

neilsayshello

Arent you missing a ( ) in the declaration of the virtual function in
class N0 ?
 
R

Rolf Magnus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top