Reputation: 7490
My code that contains function inside another function as shown below:
ReturnType OuterFunction(someParameters, MyType* mt)
{
function<OtherType*(parameterList)> innerFunction =
[this](parameterList)
{
return someOtherFunction(someParameters, mt);
}
}
I have added a parameter MyType
to someOtherFunction
and modified it's call as seen in above code.
mt
is the variable that is passed from OuterFunction
and I am not able to use it inside someOtherFunction
.
It is giving me an error
An enclosing-function local variable cannot be referenced in a lambda body unless if it is in the capture list.
Upvotes: 1
Views: 957
Reputation: 122
You have to add mt
to your capture list in the lambda youre trying to use there.
By default you have no access to outside variables. And adding this
is adding a Pointer to your object.
ReturnType OuterFunction(T s1, K s2, MyType* mt)
{
function<OtherType*(parameterList)> innerFunction =
[s1,s2,mt](parameterList)
{
return someOtherFunction(s1, s2, mt);
}
}
You can take a look at this https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=vs-2019
Upvotes: 2