C++ Boost::bind: a pointer to a bound function may only be used to call the function

StackOverflow https://stackoverflow.com/questions/18987076

  •  29-06-2022
  •  | 
  •  

문제

I would like to do something like

template<typename InstanceType>
            void add_test(void (InstanceType::* test_method )(void*),
 std::tr1::shared_ptr<InstanceType> user_test_case)
            {
                boost::function<void ()> op;
                op = boost::bind<InstanceType>(test_method, *user_test_case);

But it says:

1>d:\boost\boost/bind/mem_fn.hpp(359): error: a pointer to a bound function may only be used to call the function
1>          return (t.*f_);

What is wrong here?

도움이 되었습니까?

해결책

  1. The 1st template argument of bind is the return type. So, it should be void. Or just omit it.
  2. boost::function signature doesn't match the one of the bound function. Make it function<void(void *)>.
  3. The functor you create should accept 1 argument, so provide the appropriate argument placeholder.
  4. Finally, you can bind to the shared_ptr, directly.

The bottom line: boost::function<void (void *)> op = boost::bind(test_method, user_test_case, _1);

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top