【发布时间】:2019-07-26 07:56:40
【问题描述】:
我已经知道如何制作一个带有一个参数的递归 lambda 函数,例如计算一个数字的阶乘,但是我尝试使用 lambda 制作一个递归幂函数(作为一种实践),但是在函数中采用 2 个参数会导致错误
这是代码:
std::function <int(int)> power = [&](int a, int n)
{
return (n<=1) ? a : a*power(a, n-1);
};
return (n<=1) ? a : a*power(a, n-1); 这一行给出了这些错误:
error: no match for call to '(std::function<int(int)>) (int&, int)' note: candidate: _Res std::function<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) const [with _Res = int; _ArgTypes = {int}] note: candidate expects 1 argument, 2 provided
【问题讨论】:
标签: c++ c++11 recursion lambda std-function