'this' cannot be implicitly captured because no default capture mode has been specified

Jan 22, 2015 at 1:56 PM
Hello

My goal is to collect rest sdk functions in one class
But I get an error when I want to call extract_json method from the following block
 .then([](pplx::task<json::value> previousTask)
    {
        try
        {
            const json::value& v = previousTask.get();
            this->extract_json(v);
        }
        catch (const http_exception& e)
        {}
});
I get the following error
error C3493: 'this' cannot be implicitly captured because no default capture mode has been specified

And here is my code
pplx::task<void> Authentication::login_cpprest()
{
    http_client client(link);

    json::value postData;
    postData[L"password"] = json::value::string(charToWChar(password));
    postData[L"username"] = json::value::string(charToWChar(user_name));
 
    return client.request(methods::POST, L"", postData.to_string().c_str(), L"application/json").then([](http_response response) -> pplx::task<json::value>
    {
        if (0 == response.headers().content_type().compare(L"application/problem+json"))
            response.headers().set_content_type(L"application/json");

        json::value obj;
        switch (response.status_code())
        {
        case status_codes::OK:
            return response.extract_json();
            break;
        case status_codes::Unauthorized:
            return response.extract_json();
            break;
        default:
            break;
        }
    })
    .then([](pplx::task<json::value> previousTask)
    {
        try
        {
            const json::value& v = previousTask.get();
            this->extract_json(v);
        }
        catch (const http_exception& e)
        {
            char * error = const_cast<char*>(e.what());
        }
    });
}

void Authentication::extract_json(json::value v)
{
    if (!v.is_null())
    {
        // Loop over each element in the object
        for (auto iter = v.as_object().cbegin(); iter != v.as_object().cend(); iter++)
        {
        }
    }
}
Coordinator
Jan 22, 2015 at 4:52 PM
Hi ggiloyan89,

The error is because you don't capture any variables in your lambda function but you are using the 'this' pointer. If you add a '=' to capture by value it should compile. Keep in mind since you are using the 'this' pointer in a task continuation you will need to guarantee or make sure the instance of your Authentication class is still alive until the task has competed.

Steve
Jan 22, 2015 at 7:20 PM
stevetgates wrote:
Hi ggiloyan89,

The error is because you don't capture any variables in your lambda function but you are using the 'this' pointer. If you add a '=' to capture by value it should compile. Keep in mind since you are using the 'this' pointer in a task continuation you will need to guarantee or make sure the instance of your Authentication class is still alive until the task has competed.

Steve
Hi Steve

Thank you for reply
Can you write a little example please ?
I tryed .then([=](pplx::task<json::value> previousTask) , 'this' started to work but I get other error.
Coordinator
Jan 22, 2015 at 7:49 PM
What is the other error you get? So far I don't actually see this has having any problem with the C++ Rest SDK and more about general C++ lambda usage.

Steve
Marked as answer by ggiloyan89 on 1/22/2015 at 1:16 PM
Jan 22, 2015 at 8:17 PM
stevetgates wrote:
What is the other error you get? So far I don't actually see this has having any problem with the C++ Rest SDK and more about general C++ lambda usage.

Steve
You are right! It works. The last error was related with other code! Thanks!