Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
948 views
Welcome To Ask or Share your Answers For Others

1 Answer

To display a wstring you also need a wide version of cout - wcout.

std::wstring st = L"SomeText";
...
std::wcout << st; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...