0

I have static method in my Utils class

This is definition

/*static*/ void Utils::copy_files(void(*progress_callback)(int, int),
        std::string const & path_from,
        std::string const & path_to)
    {
      ....
    }

And here using

void TV_DepthCamAgent::progress_callback(int count, int copied_file)
{
    printf("Progress :: %d :: %d\n", count, copied_file);
}

void TV_DepthCamAgent::foo()
{
    ...
    shared::Utils::copy_files(progress_callback, path_from_copy, path_to_copy);
    ...
}

And this is an errors that I get

E0167 argument of type "void (TV_DepthCamAgent::)(int count, int copied_file)" is incompatible with parameter of type "void ()(int, int)"

Error C3867 'TV_DepthCamAgent::progress_callback': non-standard syntax; use '&' to create a pointer to member

What am I doing wrong?

Community
  • 1
  • 1
Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • 2
    Does this answer your question? [How can I pass a class member function as a callback?](https://stackoverflow.com/questions/400257/how-can-i-pass-a-class-member-function-as-a-callback) (Assuming `progress_callback` is non-static). Basically, you can't pass a member function as callback, because you need an instance to call this function on. – Lukas-T Mar 22 '20 at 18:40
  • [OT]: we now have [`std::filesysem`](https://en.cppreference.com/w/cpp/filesystem) – Jarod42 Mar 22 '20 at 18:41
  • `TV_DepthCamAgent::progress_callback` should be `static`, or `copy_files` should be modified. – Jarod42 Mar 22 '20 at 18:43
  • @churill yes, tnx – Sirop4ik Mar 22 '20 at 18:43
  • > use '&' to create a pointer to member – 273K Mar 22 '20 at 18:45

1 Answers1

3

Since you've tagged this C++ i'm assuming you want a C++ solution.

Since C++11 we can use std::function instead of the awkward C style pointer-to-function syntax.

So void(*progress_callback)(int, int) becomes std::function<void(int, int)> progress_callback

In regards to why you get that error it is because to pass a function pointer you must pass the function by reference

...
    shared::Utils::copy_files(&progress_callback);
...

You must then pass the required arguments when you call it in copy_files.

You should use std::function and std::bind for this instead of the C style you seem to be writing in

Object object
  • 1,939
  • 10
  • 19
  • o, it is new for me, tnx! but anyway callback should be static... I can't pass the class method as is, I have to pass static method, right? – Sirop4ik Mar 22 '20 at 18:49
  • no, use `std::bind(&ClassName::methodName, this)` also look up both `std::bind` and `std::function`. Also I don't know what programming background you come from (sounds like java) but in C++ functions are first class citizens and can be declared and used outside classes without needing to be static – Object object Mar 22 '20 at 18:52
  • You could also use a [lambda expression](https://en.cppreference.com/w/cpp/language/lambda) in place of `std::bind`. Often this is preferable due to lower overhead in lambdas with respect to `std::bind`. – user4581301 Mar 22 '20 at 19:06
  • Can you take a look at this question please https://stackoverflow.com/questions/60803852/how-to-use-stdbind-with-stdfunction-in-order-to-pass-method-as-a-callback – Sirop4ik Mar 22 '20 at 19:20