CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    New to VS 2012 and Lambada programming

    I am new to VC++ 2012 and Lambada programming. I want use a local initialized varaible withint he Lambada code block.

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    uri_builder uri(L"http://*:2001/");
    	http_listener listener(uri.to_uri());
    	utility::string_t strPost = L"";
    
    -------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------
    -------------------------------------------------------------------------------------------------------------------------------
    
    listener.support(methods::POST,[](http_request req)
    	{
    		std::cout << "Serving POST" << std::endl;
    		req.extract_string(true).then([req](utility::string_t body)
    		{
    			std::wcout << body << std::endl;
    			strPost = body;
    		});
    	});	
    }
    In this section the compiler throws this error

    Code:
    Error	3	error C3493: 'strPost' cannot be implicitly captured because no default capture mode has been specified
    How can I overcome this problem, how can I use it across the whole function?

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: New to VS 2012 and Lambada programming

    Ah yes, lambada programming.
    Where you kill bugs with your awesome sexy dance moves. :-)

    1) it's lambda (from the greek letter 'lambda')
    better get this right before people make fun of you.
    oh dear too late ;-)

    2)
    you're using strPost, but you have not mentioned strPost in the capture clause, nor have you specified a default capture mode (by value or by reference) for variables.
    so you get an error.

    since you're changing it, you probably want to capture by reference . IN that case add &strPost to the capture clauses (or add & to capture anything other than req by reference).

  3. #3
    Join Date
    Apr 2005
    Posts
    1,828

    Re: New to VS 2012 and Lambada programming

    Quote Originally Posted by OReubens View Post
    Ah yes, lambada programming.
    Where you kill bugs with your awesome sexy dance moves. :-)

    1) it's lambda (from the greek letter 'lambda')
    better get this right before people make fun of you.
    oh dear too late ;-)

    2)
    you're using strPost, but you have not mentioned strPost in the capture clause, nor have you specified a default capture mode (by value or by reference) for variables.
    so you get an error.

    since you're changing it, you probably want to capture by reference . IN that case add &strPost to the capture clauses (or add & to capture anything other than req by reference).
    Sorry, if I didn't explain well in the original post. I want a local variable to be access across various lambda code blocks.

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    Re: New to VS 2012 and Lambada programming

    Let’s Write a “Hello Lambda!”
    It's intentionally made enough stupid, in order to explain lambdas at beginners level.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    Re: New to VS 2012 and Lambada programming

    Quote Originally Posted by maverick786us View Post
    [...] I want a local variable to be access across various lambda code blocks.
    Beware! In your case the lambda call is asynchronous, so it's not OK to capture local variables by reference.
    Last edited by ovidiucucu; April 30th, 2015 at 01:03 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured