-2

I have this on one class:

typedef void(*PERCENTAGE_CALLBACK)(float);

And I use it on functions like this one:

int API_GenerateLayerData(int layerIndex, QByteArray data, int dataSize, PERCENTAGE_CALLBACK callBack);

But the thing is that I can't pass a parameter with a void return type and accepts float as a parameter like this:

void updateFormattingProcess(float value)
{
    emit ChangeCompose(int(value));
}

void someFunction()
{ 
    //It says cannot convert from 'void' to 'PERCENTAGE_CALLBACK'
    API_GenerateLayerData(1, data, count, updateFormattingProcess(x));
}
TerribleDog
  • 1,237
  • 1
  • 8
  • 31
  • 1
    This is `updateFormattingProcess(x)` function call, all you need is to pass pointer of this function: `&updateFormattingProcess`. – rafix07 Jul 11 '19 at 07:43
  • how do I set the value of the float in the updateFormattingProcess ? – TerribleDog Jul 11 '19 at 07:45
  • Is there a reason that you're not using [std::function](https://en.cppreference.com/w/cpp/utility/functional/function)? – Neijwiert Jul 11 '19 at 07:47
  • refer to : [How do you pass a function as a parameter in C?](https://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c) – Sander De Dycker Jul 11 '19 at 07:53

1 Answers1

3

You are trying to call the function with updateFormattingProcess(x) and pass the result of that function call (which is void) as a parameter of type void(*)(float). Instead, just pass a pointer to it:

API_GenerateLayerData(1, data, count, &updateFormattingProcess);
//                                    ^ take the addresss of the function
lubgr
  • 37,368
  • 3
  • 66
  • 117
  • how do I set the value of the float in the updateFormattingProcess ? – TerribleDog Jul 11 '19 at 07:46
  • Well, you don't. `API_GenerateLayerData` accepts a function pointer as a parameter. This suggests that it calls `updateFormattingProcess` internally and selects the right parameter?! – lubgr Jul 11 '19 at 07:46
  • it has another error '&' illegal operation bound member function expression – TerribleDog Jul 11 '19 at 07:48
  • So you call `API_GenerateLayerData` from inside a member function, and `updateFormattingsProcess` is a member function, too? – lubgr Jul 11 '19 at 07:49
  • 3
    @TerribleDog - Means your example is not a [mcve]. Please edit your question to **faithfully** represent your issue. – StoryTeller - Unslander Monica Jul 11 '19 at 07:49
  • 2
    This is probably because `updateFormattingProcess()` is non-static member of some class. You either need to do it static member or not member of any class. – sklott Jul 11 '19 at 07:51