2012-04-04 41 views
3

從這個問題故障與使用boost ::綁定和升壓::功能

How to pass class member functions to a method in a 3rd party library?

快速回顧一下是我需要的指針傳遞給函數稱爲moveset在第3類的構造函數繼黨庫的

template <class Space> 
moveset<Space>::moveset(particle<Space> (*pfInit)(rng*), 
      void (*pfNewMoves)(long, particle<Space> &,rng*), 
      int (*pfNewMCMC)(long,particle<Space> &,rng*)) 

庫提供的示例定義是簡單地定義pfInit等全局函數,讓他們打電話F,G和H。然後從控制器類中調用smc :: moveset Moveset(f,g,h);

我試圖用boost:bind來實現這個建議。不幸的是,我正在努力讓這個工作。

class IK_PFWrapper 
{ 
public: 

IK_PFWrapper(Skeleton* skeleton, PFSettings* pfSettings) ; 
smc::particle<cv_state> fInitialise(smc::rng *pRng); 

... 
} ; 
在控制器類

IK_PFWrapper testWrapper (skeleton_,pfSettings_); 
boost::function<smc::particle<cv_state> (smc::rng *)> f = boost::bind(&IK_PFWrapper::fInitialise, &testWrapper,_1) ; 

// the 2nd and 3rd argument will be eventually be defined in the same manner as the 1st 
smc::moveset<cv_state> Moveset(f, NULL, NULL); 

所得編譯器錯誤是

Algorithms\IK_PFController.cpp(88): error C2664: 'smc::moveset<Space>::moveset(smc::particle<Space> (__cdecl *)(smc::rng *),void (__cdecl *)(long,smc::particle<Space> &,smc::rng *),int (__cdecl *)(long,smc::particle<Space> &,smc::rng *))' : cannot convert parameter 1 from 'boost::function<Signature>' to 'smc::particle<Space> (__cdecl *)(smc::rng *)' 
with 
[ 
Space=cv_state 
] 
and 
[ 
Signature=smc::particle<cv_state> (smc::rng *) 
] 
and 
[ 
Space=cv_state 
] 
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 

十分讚賞

回答

0

任何幫助查看demote boost::function to a plain function pointer.

boost::function(您使用boost::bind創建的程序不會自動轉換爲普通的舊函數指針。

我建議創建一個使用boost::function,即你的榜樣(減少到一個參數)的封裝接口會看起來像:

template <class Space> 
moveset<Space>::moveset(boost::function<particle<Space> (rng*)> pfInit) 
{ 
    library_namespace::moveset<Space>(
     pfInit.target<particle<Space>(rng*)>() // parameter 1 
    ); 
} 

創建包裝意味着你只需要對付一個原始函數指針地點。 希望對代碼片段中的任何和所有錯誤有所幫助,並表示歉意!

+0

閱讀你以前的[問題](http://stackoverflow.com/questions/9997299/how-to-pass-class-member-functions-to-a-method-in-a-3rd-party-library)我不確定上述是否真正解決了原來的問題。我已經提供了關於原始問題的更多細節。 – Zero 2012-04-11 22:04:35