以下代码重现了我使用 lambda 函数遇到的错误。
#include <iostream>
#include <functional>
#include <vector>
#include <math.h>
typedef double Fct(double);
struct Function {
Function(Fct f, double r_min, double r_max, int points = 100)
{
double step = (r_max-r_min)/points;
double r = r_min;
for (int i = 0; i < points; ++i) {
y.push_back(f(r));
r += step;
}
}
void plot() const
{
for (double x:y)
std::cout << x << '\n';
}
private:
std::vector<double> y;
};
int main()
{
Function f1{[](double x){return std::cos(x);},1, 10,10};
f1.plot();
//The following does not work
int k = 2;
Function f2{[k](double x){return std::sin(k*x);},1,10,100};
f2.plot();
//Also this does not work
for (int n = 0; n < 3; ++n) {
Function f3{[n](double x){return std:sin(n*x);},1,10,10};
f3.plot();
}
}
特别是带有 lambda 函数但不捕获参数的 Function 实例 f1 可以按预期工作。另一方面,捕获 k 的实例 f2 会抛出错误:
错误:没有匹配的函数可用于调用“Function::Function()”|
实例 f3 也存在同样的问题。有人可以解释并帮助解决这个问题吗?
带有捕获的 Lambda 函数不能直接分配给函数指针 (
typedef double Fct(double);
),因为它们不能转换为常规函数指针。要解决此问题,您可以更改 Function
结构以接受 std::function<double(double)>
:
#include <iostream>
#include <functional>
#include <vector>
#include <cmath>
struct Function {
Function(std::function<double(double)> f, double r_min, double r_max, int points = 100)
{
double step = (r_max - r_min) / points;
double r = r_min;
for (int i = 0; i < points; ++i) {
y.push_back(f(r));
r += step;
}
}
void plot() const
{
for (double x : y)
std::cout << x << '\n';
}
private:
std::vector<double> y;
};
int main()
{
Function f1{[](double x) { return std::cos(x); }, 1, 10, 10};
f1.plot();
int k = 2;
Function f2{[k](double x) { return std::sin(k * x); }, 1, 10, 100};
f2.plot();
for (int n = 0; n < 3; ++n) {
Function f3{[n](double x) { return std::sin(n * x); }, 1, 10, 10};
f3.plot();
}
return 0;
}
输出:
0.540302
-0.32329
-0.942222
-0.8481
-0.112153
0.70867
0.993185
0.526078
-0.339155
-0.947722
0.909297
0.820104
0.704411
0.565956
0.409214
0.239249
0.0615537
-0.118131
-0.293998
-0.460366
-0.611858
-0.743579
-0.851273
-0.931461
-0.98155
-0.999923
-0.985986
-0.940189
-0.864012
-0.759917
-0.631267
-0.482218
-0.317589
-0.142697
0.0368064
0.21512
0.386483
0.545357
0.686609
0.805675
0.898708
0.962701
0.995587
0.996303
0.964825
0.902172
0.810367
0.692377
0.552014
0.393815
0.22289
0.044763
-0.13481
-0.310028
-0.475227
-0.625071
-0.754717
-0.859976
-0.937447
-0.984626
-0.99999
-0.983042
-0.934329
-0.855425
-0.74888
-0.618137
-0.46742
-0.3016
-0.126035
0.0536037
0.23151
0.401935
0.559373
0.698736
0.815521
0.905955
0.967114
0.997024
0.994717
0.960269
0.894791
0.800401
0.680147
0.537916
0.378304
0.206467
0.0279596
-0.151452
-0.325969
-0.489954
-0.638107
-0.765641
-0.868435
-0.943168
-0.987424
-0.999774
-0.979819
-0.928204
-0.846596
-0.737632
-0.604833
-0.45249
-0.285527
-0.109337
0.0703858
0.247834
0.417274
0.573231
0.710666
0.825137
0
0
0
0
0
0
0
0
0
0
0.841471
0.9463
0.334988
-0.529836
-0.993691
-0.70554
0.116549
0.850437
0.940731
0.319098
0.909297
-0.611858
-0.631267
0.898708
0.22289
-0.99999
0.23151
0.894791
-0.638107
-0.604833