c++ - an enclosing-function local variable cannot be referenced in a lambda body unless if it is in capture list -
I have the json :: value object and I try to get the value in a straight but I'm about the capture list I get this error in I understand that this bracket holds the [] capture list in the phrase, but I can not understand how I can return a value in the Lambda function?
Zero Jasondomain :: Set Value (JSN :: Value OBG) {weather.coord.lon = obj.at (L "Coordination"). (L "longitude") as_double () .; Weather.coord.lat = obj.at (L "Coordinate"). (L "Latitude"). As_double (); } Zero JsonDeneme :: getHttp () {// json :: value val; Http_client Client (U ("http://api.openweathermap.org/data/2.5/weather?q=Ankara,TR")); Client.request (methods :: GET) .then ([] (http_response response) - & gt; pplx :: task & lt; json :: value & gt; {if (response.status_code) == condition_code :: OK ) {Printf ("Received response status code:% u \ n", response.status_code ()); Return response .extract_json ();} return pplx :: task_from_result (json :: value ());}). Then ([] (PPLX: Work: PreviousTask) {try {json :: value v = previousTask.get (); Set value (v); // ------ ------------ -----------------------} (http_exception const & amp; a) {wcout & lt; & Lt; e.what () & lt; endl;}}) .Request (); }
The capture-list that you put in the middle of the square bracket See:
void foo () {int i = 0; [] () {I + = 2; }} Here Lambda does not capture anything, thus it will not reach the enclosed area, and it will not know what is i now, Capture everything from the context:
void foo () {int i = 0; [& Amp;] () // Note & amp ;. This means that we are capturing all the attached variables of reference {i + = 2; } Cout & lt; & Lt; 2; } In this example, the reference in the attached area to the i inside the lambda is i .
In your example, you have a lambda within a member- the function of an object. You are trying to call the object's function: setValues (v) , but your capture list is empty, so your lambda does not know that setValues Now, if you capture this in Lambda, then Lambda will have access to all the objects of the object, because setValues (v) is similar < Code> this- & gt; Set Value (v) In your case, the error will end.
Comments
Post a Comment