I had the code:
std::string st = "SomeText"; ... std::cout << st;
and that worked fine. But now my team wants to move to wstring
. So I tried:
std::wstring st = "SomeText"; ... std::cout << st;
but this gave me a compilation error:
Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'const char [8]' to 'const std::basic_string<_Elem,_Traits,_Ax> &' D:...\TestModule1.cpp 28 1 TestModule1
After searching the web I read that I should define it as:
std::wstring st = L"SomeText"; // Notice the "L" ... std::cout << st;
this compiled but prints "0000000000012342"
instead of "SomeText"
.
What am I doing wrong ?
Another way to print wide string: std::wstring str1 = L"SomeText"; std::wstring strr2(L"OtherText!"); printf("Wide String1- %ls \n", str1. c_str()); wprintf(L"Wide String2- %s \n", str2.
This function is used to convert the numerical value to the wide string i.e. it parses a numerical value of datatypes (int, long long, float, double ) to a wide string. It returns a wide string of data type wstring representing the numerical value passed in the function.
To display a wstring you also need a wide version of cout - wcout.
std::wstring st = L"SomeText"; ... std::wcout << st;
Use std::wcout
instead of std::cout
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With