我需要从其参数中调用不同函数的函数。
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();
}
答案 0 :(得分:0)
首先,为函数指针创建一个typedef:
typedef bool(*test_LST)(LST*);
然后更改每个功能的签名:
void For_Each_el(LST *&el, test_LST f)
最后改变你在main中的称呼方式:
For_Each_el(Head, Is_Even);
您也可以使用typedef std::function<bool(LST*)> test_LST;
代替上述typedef来允许每次迭代的函数对象,或者将For_Each_el
写为template
函数。