2016-02-02 51 views
0

傳遞給boost函數的參數數量不匹配我想創建一個使用boost函數和綁定函數,但我不能只傳遞一個參數到目標函數有3個參數:使用boost :: bind

#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 

當然,我想通過只使用一個參數,因爲我希望綁定採取其他2個args來照顧。我試圖尋找我的錯誤,但我找不到任何(在短時間內)。 你能幫我找到問題嗎? 謝謝

+0

'auto functor_ = ...' – Drop

回答

2

您正在創建boost::function<void(int&, int&, int&)>,即函數,它帶有三個參數。既然你使用綁定並且想用一個參數調用函數,它應該只是boost::function<void(int&)>