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

    Input strings in VC++ 2012

    I have a source code

    Code:
    listener.support(methods::GET, [](http_request req)
        {
            std::cout << "Serving GET" << std::endl;
    		//req.reply(status_codes::OK, U("input string!"), U("text/html"));
            req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a GET Request.)</body></html>"), U("text/html"));
        });
    Now I want to take some input from the user and then post it in req.reply. For that I tried this

    Code:
    std::string strName; 
    std::cin >> strName; 
    
    req.reply(status_codes::OK, strName, U("text/html"));
    But somehow the compiler throws this error.

    1>Listener.cpp(36): error C3493: 'strName' cannot be implicitly captured because no default capture mode has been specified
    1>Listener.cpp(36): error C2664: 'pplx::task<void> web::http::http_request::reply(web::http::status_code,const utility::string_t &,utility::string_t) const' : cannot convert parameter 2 from 'std::string' to 'const utility::string_t &'
    1> Reason: cannot convert from 'std::string' to 'const utility::string_t'
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Input strings in VC++ 2012

    See my reply in your another thread (about possible conversion)
    Victor Nijegorodov

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

    Re: Input strings in VC++ 2012

    [ off-topic ]
    Probably, we have to ask for a new forum section named "VC++ 2012 & C++ REST SDK (codename Casablanca)".

    [ on-topic ]
    Have a look in ...\casablanca\include\cpprest\basic_types.h and you'll find
    Code:
    namespace utility
    {
    
       // ...
    
    #ifdef _UTF16_STRINGS
    //
    // On Windows, all strings are wide
    //
    typedef wchar_t char_t ;
    typedef std::wstring string_t;
    #define _XPLATSTR(x) L ## x
    typedef std::wostringstream ostringstream_t;
    typedef std::wofstream ofstream_t;
    typedef std::wostream ostream_t;
    typedef std::wistream istream_t;
    typedef std::wifstream ifstream_t;
    typedef std::wistringstream istringstream_t;
    typedef std::wstringstream stringstream_t;
    #define ucout std::wcout
    #define ucin std::wcin
    #define ucerr std::wcerr
    #else
    //
    // On POSIX platforms, all strings are narrow
    //
    typedef char char_t;
    typedef std::string string_t;
    #define _XPLATSTR(x) x
    typedef std::ostringstream ostringstream_t;
    typedef std::ofstream ofstream_t;
    typedef std::ostream ostream_t;
    typedef std::istream istream_t;
    typedef std::ifstream ifstream_t;
    typedef std::istringstream istringstream_t;
    typedef std::stringstream stringstream_t;
    #define ucout std::cout
    #define ucin std::cin
    #define ucerr std::cerr
    #endif // endif _UTF16_STRINGS
    
       // ...
    
    }// namespace utility
    So, to avoid conversion errors (and make cross-platform code) when deal with C++ REST SDK, use C++ REST SDK types: utility::string_t instead of std::string, utility::ucin and not std::cin, and so on.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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

    Re: Input strings in VC++ 2012

    Quote Originally Posted by ovidiucucu View Post
    [ off-topic ]
    Probably, we have to ask for a new forum section named "VC++ 2012 & C++ REST SDK (codename Casablanca)".

    [ on-topic ]
    Have a look in ...\casablanca\include\cpprest\basic_types.h and you'll find
    Code:
    namespace utility
    {
    
       // ...
    
    #ifdef _UTF16_STRINGS
    //
    // On Windows, all strings are wide
    //
    typedef wchar_t char_t ;
    typedef std::wstring string_t;
    #define _XPLATSTR(x) L ## x
    typedef std::wostringstream ostringstream_t;
    typedef std::wofstream ofstream_t;
    typedef std::wostream ostream_t;
    typedef std::wistream istream_t;
    typedef std::wifstream ifstream_t;
    typedef std::wistringstream istringstream_t;
    typedef std::wstringstream stringstream_t;
    #define ucout std::wcout
    #define ucin std::wcin
    #define ucerr std::wcerr
    #else
    //
    // On POSIX platforms, all strings are narrow
    //
    typedef char char_t;
    typedef std::string string_t;
    #define _XPLATSTR(x) x
    typedef std::ostringstream ostringstream_t;
    typedef std::ofstream ofstream_t;
    typedef std::ostream ostream_t;
    typedef std::istream istream_t;
    typedef std::ifstream ifstream_t;
    typedef std::istringstream istringstream_t;
    typedef std::stringstream stringstream_t;
    #define ucout std::cout
    #define ucin std::cin
    #define ucerr std::cerr
    #endif // endif _UTF16_STRINGS
    
       // ...
    
    }// namespace utility
    So, to avoid conversion errors (and make cross-platform code) when deal with C++ REST SDK, use C++ REST SDK types: utility::string_t instead of std::string, utility::ucin and not std::cin, and so on.
    In my case utility::ucin is not available. When std::cout << "Serving GET" << std::endl; is working fine, cin should work fine too but how will I convert that string to something similar to U("") ?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Input strings in VC++ 2012

    Quote Originally Posted by maverick786us View Post
    In my case utility::ucin is not available.
    Define "not available".
    Victor Nijegorodov

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

    Re: Input strings in VC++ 2012

    Quote Originally Posted by VictorN View Post
    Define "not available".
    When I type utility:: ucin is not there in the dropdown list of its members, as if its not a member of utility.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Input strings in VC++ 2012

    ucin is a definition (by a #define), not a class member so won't show in the dropdown list.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    Re: Input strings in VC++ 2012

    ...and about C3493
    Let's try to write a simple "Hello lambda!" program!

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string hello_lambda = "Hello lambda!";
        auto say_hello = [] {std::cout << hello_lambda; };
        say_hello();
    }
    This generates compiler error C3493:
    Code:
    error C3493: 'hello_lambda' cannot be implicitly captured because no default capture mode has been specified
    That's because the lambda-introducer ([]) is empty, meaning "nothing to capture".
    To get rid of that error, we have to specify capture mode, as follows:
    Code:
        std::string hello_lambda = "Hello lambda!";
        // hello_lambda variable is captured by value
        auto say_hello = [hello_lambda] {std::cout << hello_lambda; };
        say_hello();
    or
    Code:
        std::string hello_lambda = "Hello lambda!";
        // hello_lambda variable is captured by reference
        auto say_hello = [&hello_lambda] {std::cout << hello_lambda; };
        say_hello();
    or
    Code:
        std::string hello_lambda = "Hello lambda!";
        // all variables are captured by value
        auto say_hello = [=] {std::cout << hello_lambda; };
        say_hello();
    or
    Code:
        std::string hello_lambda = "Hello lambda!";
        // all variables are captured by reference
        auto say_hello = [&] {std::cout << hello_lambda; };
        say_hello();
    or...

    I would like to suggest: before copy-pasting then modifying code examples that use C++ lambda expressions, try to understand them.
    There are tons of articles about C++11 new features including lambda over the Internet.
    Just pointing to few examples:

    May look weird but at last they are not so much "rocket science".
    Last edited by ovidiucucu; October 7th, 2014 at 05:27 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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

    Re: Input strings in VC++ 2012

    ...and about U("...")

    U(x) is a macro, also defined in basic_types.h header file of C++ REST SDK.

    Code:
    #ifdef _MS_WINDOWS
    #define _UTF16_STRINGS
    #endif
        // ...
    #ifdef _UTF16_STRINGS
    //
    // On Windows, all strings are wide
    //
        // ...
    #define _XPLATSTR(x) L ## x
        // ...
    #else
    //
    // On POSIX platforms, all strings are narrow
    //
        //... 
    #define _XPLATSTR(x) x
        // ...
    #endif // endif _UTF16_STRINGS
    
    #ifndef _TURN_OFF_PLATFORM_STRING
    #define U(x) _XPLATSTR(x)
    #endif // !_TURN_OFF_PLATFORM_STRING
    In a build for the Windows platform, U("...") expands to a wide string literal, L"...".
    For the other platforms U("...") expands to a narrow string literal, "...".
    Last edited by ovidiucucu; October 7th, 2014 at 06:19 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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

    Re: Input strings in VC++ 2012

    Quote Originally Posted by ovidiucucu View Post
    ...and about U("...")

    U(x) is a macro, also defined in basic_types.h header file of C++ REST SDK.

    Code:
    #ifdef _MS_WINDOWS
    #define _UTF16_STRINGS
    #endif
        // ...
    #ifdef _UTF16_STRINGS
    //
    // On Windows, all strings are wide
    //
        // ...
    #define _XPLATSTR(x) L ## x
        // ...
    #else
    //
    // On POSIX platforms, all strings are narrow
    //
        //... 
    #define _XPLATSTR(x) x
        // ...
    #endif // endif _UTF16_STRINGS
    
    #ifndef _TURN_OFF_PLATFORM_STRING
    #define U(x) _XPLATSTR(x)
    #endif // !_TURN_OFF_PLATFORM_STRING
    In a build for the Windows platform, U("...") expands to a wide string literal, L"...".
    For the other platforms U("...") expands to a narrow string literal, "...".
    So when I have declared this thing.

    Code:
    	utility::string_t strName1;
    	ucin >> strName1;
    How can I convert it into this U, so that i can use it here...

    Code:
    req.reply(status_codes::OK, U("input string!"), U("text/html"));
    So that I can use it instead of hard coded "input string"?

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

    Re: Input strings in VC++ 2012

    Quote Originally Posted by maverick786us View Post
    So when I have declared this thing.

    Code:
    	utility::string_t strName1;
    	ucin >> strName1;
    How can I convert it into this U, so that i can use it here...

    Code:
    req.reply(status_codes::OK, U("input string!"), U("text/html"));
    So that I can use it instead of hard coded "input string"?
    Why not?
    Completing the suggestion from post #8: before copy-pasting then modifying code examples containing a call to C++ REST SDK function http_request::reply, try to find out what type of arguments it takes.
    For that you can simply do the following:
    • in the text code editor, right click on reply token and choose "Peek definition", "Go to definition" or "Go to declaration" or
    • select reply token then hit F12 key or
    • have a look into C++ REST SDK documentation, also available on-line http://microsoft.github.io/cpprestsd...a6c51e3203d577.


    You will find something like this
    Code:
    pplx::task<void> reply(http::status_code status, 
                           const utility::string_t &body_data, 
                           utility::string_t content_type = _XPLATSTR("text/plain")) const
    As long as the second argument is of type const utility::string_t&, you can pass strName1 which is of type utility::string_t, with not any explicit conversion.
    Last edited by ovidiucucu; October 7th, 2014 at 07:30 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