locked
_wtof_l RRS feed

  • Question

  • Why doesn't the following code compile in Visual Studio 2013 ??

    DOUBLE dval(std::wstring szval)
    {
         return _wtof_l(szval);
    }

    I have the declaration in another file called <nameofcppfile.h>

    DOUBLE dval(std::wstring);

    The documentation says it should return a double, so I don't see a problem...

    Tuesday, February 17, 2015 10:49 PM

Answers

  • I just tried:

    DOUBLE dval(std::wstring szval)
    {
         return _wtof(szval.c_str());
    }

    and it compiled without any errors...


    • Marked as answer by Shu 2017 Wednesday, February 18, 2015 7:34 AM
    • Edited by TallGuy63 Wednesday, February 18, 2015 3:43 PM
    Tuesday, February 17, 2015 11:06 PM

All replies

  • Why don't you tell us what the error message is.

    My help function says _wtof_l takes two arguments.  You are passing only one. 

    If you don't want to specify the locale, use _wtof.

    Tuesday, February 17, 2015 10:59 PM
  • DOUBLE dval(std::wstring szval)
    {
         return _wtof(szval);
    }

    returns an error also:

    Error    2    error C2664: 'double _wtof(const wchar_t *)' : cannot convert argument 1 from 'std::wstring' to 'const wchar_t *'   
        3    IntelliSense: no suitable conversion function from "std::wstring" to "const wchar_t *" exists   

    Tuesday, February 17, 2015 11:03 PM
  • I just tried:

    DOUBLE dval(std::wstring szval)
    {
         return _wtof(szval.c_str());
    }

    and it compiled without any errors...


    • Marked as answer by Shu 2017 Wednesday, February 18, 2015 7:34 AM
    • Edited by TallGuy63 Wednesday, February 18, 2015 3:43 PM
    Tuesday, February 17, 2015 11:06 PM
  • On 2/17/2015 6:06 PM, TallGuy63 wrote:

    why is the .c_str() needed?? the function is stated to convert a wstring to a double...

    Stated where and by whom? The documentation for _wtof never once mentions wstring.


    Igor Tandetnik
    • Marked as answer by Shu 2017 Wednesday, February 18, 2015 7:34 AM
    • Unmarked as answer by TallGuy63 Wednesday, February 18, 2015 3:42 PM
    Wednesday, February 18, 2015 12:06 AM
  • Now that I look back at it, you're right Igor...

    I guess my question SHOULD be why is Microsoft's documentation so vague??

    LOL

    Oh well... thanks anyway

    Wednesday, February 18, 2015 12:14 AM
  • On 18/02/2015 01:14, TallGuy63 wrote:

    Now that I look back at it, you're right Igor...

    I guess my question SHOULD be why is Microsoft's documentation so vague??

    I think the MSDN documentation for _wtof() is very clear about the function taking just one parameter of type "const wchar_t*":

    https://msdn.microsoft.com/en-us/library/hc25t012.aspx

    double _wtof(
         const wchar_t *str
    );
     

     Since there is no implicit conversion operator available for std::wstring to convert it to a "const wchar_t*", you can call the std::wstring::c_str() method to get the a pointer to a NUL-terminated C-style string, and pass it to _wtof().

    Giovanni

    Thursday, February 19, 2015 6:00 PM