несоответствие количества аргументов, переданных функции повышения с помощью boost::bind

Я пытаюсь создать Functor, используя функцию boost и привязку, но я не могу передать только один аргумент функции назначения, которая имеет 3 аргумента:

#include <boost/bind.hpp>
#include <boost/function.hpp>


template <typename Functor>
void foreach(Functor f) 
{
  int k;
  f(k);
}


class A
{
private:
void bar(int &a,int &b,int& c)
{}

void foo()
{
 int keys,predicate;
boost::function<void(int &,int&,int&)> functor_ ( boost::bind(
                &A::bar,
                this,
                boost::ref(keys),
                boost::ref(predicate),
                _1
                ));
 foreach(functor_);
 }

}; 

Ошибка говорит:

binf.cpp: In instantiation of ‘void foreach(Functor) [with Functor = boost::function<void(int&, int&, int&)>]’:
binf.cpp:29:18:   required from here
binf.cpp:9:6: error: no match for call to ‘(boost::function<void(int&, int&, int&)>) (int&)’
   f(k);
      ^
In file included from /usr/include/boost/function/detail/maybe_include.hpp:28:0,
                 from /usr/include/boost/function/detail/function_iterate.hpp:14,
                 from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:62,
                 from /usr/include/boost/function.hpp:64,
                 from binf.cpp:2:
/usr/include/boost/function/function_template.hpp:1048:7: note: candidate is:
 class function<BOOST_FUNCTION_PARTIAL_SPEC>
       ^
/usr/include/boost/function/function_template.hpp:761:17: note: boost::function3<R, T1, T2, T3>::result_type boost::function3<R, T1, T2, T3>::operator()(T0, T1, T2) const [with R = void; T0 = int&; T1 = int&; T2 = int&; boost::function3<R, T1, T2, T3>::result_type = void]
     result_type operator()(BOOST_FUNCTION_PARMS) const
                 ^
/usr/include/boost/function/function_template.hpp:761:17: note:   candidate expects 3 arguments, 1 provided

Конечно, я хочу передать только один аргумент, потому что я ожидаю, что bind позаботится о двух других аргументах. Я пытался найти свою ошибку, но ничего не нашел (в коротком звонке). Не могли бы вы помочь мне найти проблему? Спасибо


person rahman    schedule 02.02.2016    source источник


Ответы (1)


Вы создаете boost::function<void(int&, int&, int&)>, то есть функцию, которая принимает три аргумента. Поскольку вы используете привязку и хотите вызвать функцию с одним аргументом, это должно быть просто boost::function<void(int&)>.

person ForEveR    schedule 02.02.2016