-1

I wanted to pass a function taking as argument and returning a void * to another function. Right now i have the following code:

MyClass *MyClass::bind(std::function<void*(void *)> fun) {
    this->value = fun(this->value);
    return this;
}

value is a void * variable inside MyClass, i can't figure out how to pass a function to my function bind.
When i try to pass it the name of a function like this

auto *class = new MyClass();
class->bind(fooFunction);

i get the error

no viable conversion from '<overloaded function type>' to 'std::function<void *(void *)>'

Is there any way to achieve what i want with this code or does an easier approach exist?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
John Doe
  • 1,613
  • 1
  • 17
  • 35
  • 3
    Your problem is not in the code shown, but rather in code you chose not to show. Type a [MCVE] that reproduces the error and otherwise compiles yet is minimal and share that. Your variable names are illegal, so your pseudo-code cannot be code for completely unrelated reasons. – Yakk - Adam Nevraumont Oct 27 '17 at 15:18

1 Answers1

2

You have more than one overload of fooFunction.

Use a function name with only one overload (that matches the signature).

Alternatively, wrap it in a lambda:

auto *bob = new MyClass();
bob->bind([](void*ptr)->void*{return fooFunction(ptr);});

and rely on stateless lambda implicit cast to function pointer.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • yeah i wrote the wrong type in the function definition so i had two functions with the same name in the code. I have another question but i'll make a new question with more details in the future. – John Doe Oct 27 '17 at 15:21