-1

I'm trying to call a function pointer that points to a member function and I either get the error

Error 3 error C2064: term does not evaluate to a function taking 0 arguments

or

Error 3 error C2171: '*' : illegal on operands of type 'Foo::func_t

my code looks like

class Foo
{
   void stuff();
   void more_stuff();
   void my_func();

   typedef void(Foo::* func_t)(void);
   func_t fptr;
};

void Foo::my_func()
{
   //do some stuff
}

void Foo::stuff()
{
   fptr = &Foo::my_func;
}

void Foo::more_stuff()
{
   if(fptr != 0)
   {
       (*fptr)(); //error C2171: '*' : illegal on operands of type 'Foo::func_t
       (fptr)(); //term does not evaluate to a function taking 0 arguments
   }
}

Can anyone see the issue here?

Thundercleez
  • 327
  • 1
  • 4
  • 18
  • 3
    And which object of the class will the function have for `this`? You haven't specified any such object. – chris May 10 '15 at 21:20

1 Answers1

0

The correct syntax is

(this->*fptr)();

This is required since it's a member function pointer and you need to explicitly provide an instance to work on when using fptr. It might look like the compiler could already use the implicit *this, but that is not what the standard says and hence you need to take care of it manually.

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • Wow, that's tricky. I thought this was always implicitly called on everything done in a class. But sure enough, that got it to compile. – Thundercleez May 10 '15 at 21:46