2011-03-14 74 views
0

我無法弄清楚什麼是錯這裏的代碼,所以我hopinh有人可以幫助我:提升功能問題

在頭文件中,我定義了以下功能

void GenLiveVar(const instr_ptr instr, std::vector<ResultBase*> &list); 
void KillLiveVar(const instr_ptr instr, std::vector<ResultBase*> &list); 

在類的頭文件,我定義這個的typedef:

typedef boost::function<void(instr_ptr, std::vector<ResultBase*>) > GenFunction; 

類裏面,我有GenFunction

的兩個實例
GenFunction Gen; 
GenFunction Kill; 

我分配到他們如下

void DataFlowSolver::SetGenFunction(GenFunction &func) 
{ 
    Gen = func; 
} 

void DataFlowSolver::SetKillFunction(GenFunction &func) 
{ 
    Kill = func; 
} 

在我的節目後,我分配到根如下:

blockSolver.SetGenFunction(GenLiveVar); 

其中GenLiveVar較早的功能提到和blockSolver是一個實例持有Gen/Kill的階級。裏面blockSolver,我做到以下幾點:

std::vector<ResultBase*> genList; 
Gen(currentBlock->GetInstrPtr(i), &genList); 

和GetInstrPtr被定義爲const instr_ptr GetInstrPtr(int index);

它生成follwoing錯誤(抱歉長)

no match for call to '(GenFunction) (const instr_ptr, std::vector<ResultBase*, std::allocator<ResultBase*> >*)' 
    /nfs/ug/homes-2/r/rileyjon/ece540/Final/boost/function/function_template.hpp:1007: 
note: candidates are: typename boost::function2<R, T1, T2>::result_type boost::function2<R, T1, T2>::operator()(T0, T1) const [with R = void, T0 = boost::shared_ptr<Instruction>, 
T1 = std::vector<ResultBase*, std::allocator<ResultBase*> > 

] 

我真的不明白爲什麼這是一個問題:類型絕對相同。一些幫助將不勝感激。謝謝

回答

4

的種類絕對是不是一樣。

要傳遞的參數是std::vector<ResultBase*>*類型(指針)的:

Gen(currentBlock->GetInstrPtr(i), &genList); 
           ^

對應的參數是std::vector<ResultBase*>類型(的值)的:

boost::function<void(instr_ptr, std::vector<ResultBase*>)> 
                ^

還要注意在參數的不匹配boost::function(按值接受第二個參數)和分配給它的兩個函數(通過引用獲取第二個參數)之間的類型。這可能不會給你你期望的行爲。

+0

哦..好吧,我現在覺得啞巴,謝謝! – Megatron 2011-03-14 03:02:43

+0

不要太笨。我想我們都會比我們想承認的更頻繁地做這些事情。 (除了我自己,我的意思是......我的代碼總是沒有「愚蠢的」錯誤。):) – dappawit 2011-03-14 03:18:22