2014-09-30 70 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

相关问题