-4

Inside some class called Light, I have a static function.

I would like to "fire" a delegate from it ,

Inside Light.h

static float intepreterDelegate(char *arg){



        // here I need to call the function pointer inside Light itself
        Light b;
        return b.fpAction(arg); //  ** error: "expected unqualified id"
        };

    float (*fpAction)(char*) = 0 ; // the actual pointer 

How would I create the right syntax for this ?

b.(*fpAction)("arg");

EDIT:

(b.*b.fpAction)(arg);

ERROR: right hand operator to * has non.

Curnelious
  • 1
  • 16
  • 76
  • 150
  • I don't know what MVCE error is, not the whole world is a c++ programmers. You have a concrete answer ? – Curnelious Aug 04 '16 at 10:03
  • You need a member function pointer, and you have to declare it before using it inside the `interpretDelegate` function. – Quentin Aug 04 '16 at 10:03
  • @Quentin can you explain ? – Curnelious Aug 04 '16 at 10:04
  • 1
    @Curnelious He was asking, that you supply us with [mcve]. – Algirdas Preidžius Aug 04 '16 at 10:04
  • 2
    Possible Duplicate or Related: [How to invoke pointer to member function when it's a class data member?](http://stackoverflow.com/q/6316751/514235) – iammilind Aug 04 '16 at 10:07
  • @iammilind yea a possible duplicate, but (b.*b.fpAction)(arg); still gives an error. – Curnelious Aug 04 '16 at 10:14
  • 1
    Possible duplicate of [How to invoke pointer to member function from static member function?](http://stackoverflow.com/questions/26604738/how-to-invoke-pointer-to-member-function-from-static-member-function) – Garf365 Aug 04 '16 at 10:15
  • I don't understand what your code actually is. You say `intepreterDelegate` is inside a class, but what you have posted doesn't appear to be. I assume there is more definition around this you haven't shared. Next you seem to be having trouble with `return b.fpAction(arg);`. But I don't know how `fpAction` is defined, or where because there aren't enough details in the question. ""expected unqualified id"" often happens because of missing or extra semicolons or similar, so there's not enough info to answer this – doctorlove Aug 04 '16 at 10:16

2 Answers2

1

You have the wrong type:

float (*fpAction)(char*) = 0 ; // the actual pointer

Should be

float (Light::*fpAction)(char*) = 0 ; // the actual pointer

and then

fpAction = &Light::myMethod;

and

static float intepreterDelegate(char *arg){
    Light b;
    return (b.*fpAction)(arg);
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Thank you very much , but : return (b.*fpAction)(arg); gives error:" invalid use of faction" , even if I change to exactly what you have showed here. – Curnelious Aug 04 '16 at 10:32
1
float (*fpAction)(char*) = 0 ; // the actual pointer 

this creates a regular function pointer, not a member-function pointer. change to

float (Light::*fpAction)(char*) = 0 ;

calling this function pointer on the Light instance named b

float result = (b.*b.fpAction)("arg");

P.S. if you're wondering what the double b is doing there. it's really (b.*(b.fpAction))("arg"); b.fpAction identifies the pointer as member of Light instance b. (b.*pointer)("arg") calls the functionpointer using 'b' as 'this' value inside the function.

oreubens
  • 329
  • 1
  • 9
  • Thank you! this worked , but now setting the delegate like this : void Light::setDelegate( float(*fp)(char*)) { fpAction=fp; } gives error, I guess I am doing it wrong. – Curnelious Aug 04 '16 at 10:38
  • you need to change the delegate call to also use the proper prototype for a member-function pointer: float (Light::*fpAction)(char*) – oreubens Aug 04 '16 at 10:45
  • Thanks, you really helped me. Can you please show how to set my delegate right ? ( I am very new to C++) , this is the delegate function void Light::setDelegate( float(fp)(char)) { set here } – Curnelious Aug 04 '16 at 10:47