2014-09-30 68 views
0

我需要從它的參數調用不同函數的函數。從另一個函數的參數調用函數

class LST { 
public: 
    char *value; 
    LST *Next; 
}; 
bool Is_Even(LST *el) { 
    return true; 
}  
void For_Each_el(LST *&el, bool f) { 
    LST *H = el; 
    while (H) { 
    if (f(H)) //this line causes the error 
     Current = H; 
    H = H->Next; 
    } 
} 

以下是錯誤:

error C2064: the result of evaluating the fragment is not a function that takes one argument 

(譯自俄文)

所以,這個代碼不起作用。

這是我如何把它在main()功能:

int main() { 
    Head = new LST; 
    Head->value = "4"; 
    For_Each_el(Head, Is_Even(Head)); 
    _getch(); 
} 
+1

'bool f'是一個名爲'f'的布爾參數。你稱它爲一個函數指針。另外值得注意的是:你沒有/顯示一個構造函數,所以你不能指望'Next'被初始化爲'nullptr',並且如果你想'value'是一個字符串,可以使用'std :: string' 。 – crashmstr 2014-09-30 19:24:00

+0

@ GALIAF95你的C++代碼看起來很像C代碼。也許一本書或一個教程是一個好主意。 – Biffen 2014-09-30 19:26:49

回答

0

首先,創建一個typedef函數指針:

typedef bool(*test_LST)(LST*); 

然後更改您的簽名爲每個功能:

void For_Each_el(LST *&el, test_LST f) 

終於改變你如何在主要中調用它:

For_Each_el(Head, Is_Even); 

也可以使用一個typedef std::function<bool(LST*)> test_LST;代替上述的typedef以允許換每次迭代函數對象,或寫For_Each_el作爲template功能。

+0

謝謝你!!!!!! – GALIAF95 2014-09-30 19:31:53

相關問題