I have a static
method my_method_1()
in my_class
, and I am trying to use it in a lambda:
我在my_class中有一个静态方法my_method_1(),我试图在lambda中使用它:
static void my_method_1(el);
void my_class::my_method_2()
{
std::for_each(my_list_.begin(), my_list_.end(),
[](auto& element)
{
my_method_1(element);
});
}
gcc6 gives me an error:
gcc6给了我一个错误:
'this' was not captured for this lambda function
这个lambda函数没有捕获'this'
In gcc4, it compiles.
在gcc4中,它编译。
2 个解决方案
#1
0
Cannot reproduce.
According the error ("error: ‘this’ was not captured for this lambda function") my_method_1()
isn't static
.
根据错误(“错误:'没有为此lambda函数捕获此'”)my_method_1()不是静态的。
If my_method_1()
is a non-static method, you can use it inside a lambda capturing this
by value (that is like capturing the object by reference); something like
如果my_method_1()是一个非静态方法,你可以在lambda中使用它来捕获这个值(就像通过引用捕获对象);就像是
// v <- capture by value
[=](auto& element)
{ my_method_1(element); }
If my_method_1()
is really a static
method, please prepare a minimal but complete example to reproduce your problem.
如果my_method_1()实际上是一个静态方法,请准备一个最小但完整的示例来重现您的问题。
#2
0
2 observations:
-
your function is static, you can refer to it as
my_class::my_method_1()
你的函数是静态的,你可以将它称为my_class :: my_method_1()
-
You don't need to use a lambda here, have you tried this ?
你不需要在这里使用lambda,你试过这个吗?
void my_class::my_method_2() { for (auto& element : my_list) my_method_1(element); }
#1
0
Cannot reproduce.
According the error ("error: ‘this’ was not captured for this lambda function") my_method_1()
isn't static
.
根据错误(“错误:'没有为此lambda函数捕获此'”)my_method_1()不是静态的。
If my_method_1()
is a non-static method, you can use it inside a lambda capturing this
by value (that is like capturing the object by reference); something like
如果my_method_1()是一个非静态方法,你可以在lambda中使用它来捕获这个值(就像通过引用捕获对象);就像是
// v <- capture by value
[=](auto& element)
{ my_method_1(element); }
If my_method_1()
is really a static
method, please prepare a minimal but complete example to reproduce your problem.
如果my_method_1()实际上是一个静态方法,请准备一个最小但完整的示例来重现您的问题。
#2
0
2 observations:
-
your function is static, you can refer to it as
my_class::my_method_1()
你的函数是静态的,你可以将它称为my_class :: my_method_1()
-
You don't need to use a lambda here, have you tried this ?
你不需要在这里使用lambda,你试过这个吗?
void my_class::my_method_2() { for (auto& element : my_list) my_method_1(element); }