2

I'm using Code Blocks to write a C++ program. I want to put a Hebrew string in an std::wstring type, but it isn't working.

If I write this:

std::wstring str2("שלום");

and compile, it gives me this error:

error: no matching function for call to 'std::basic_string<wchar_t>::basic_string(const char [5])'|

If I use "L" before the string literal, i.e.

std::wstring str2(L"שלום");

then it gives the error:

error: converting to execution character set: Invalid argument|
einpoklum
  • 118,144
  • 57
  • 340
  • 684

2 Answers2

3

Wide string literals have the form L"peace". The leading L is what tells the compiler it's a wchar_t instead of char.

See: http://en.cppreference.com/w/cpp/language/string_literal

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

i got it.

i use L before like: str2 (L"שלום");

Set up CodeBlocks editor by clicking Settings then Editor then Encoding settings, and select UTF-8. You probably want to select "As default encoding" and unselect everything else. You won't have to pass -finput-charset to the compiler because UTF-8 is the default

  • 2
    Don't use iso-8859-8. Use UTF-8 like 99% of the world. Set up CodeBlocks editor by clicking Settings then Editor then Encoding settings, and select UTF-8. You probably want to select "As default encoding" and unselect everything else. You won't have to pass -finput-charset to the compiler because UTF-8 is the default. – n. m. could be an AI Feb 20 '17 at 08:03
  • אתה תמיד מוזמן. Also please read [this](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/) – n. m. could be an AI Feb 20 '17 at 09:51
  • @רועינוה: If you've accepted the suggestion to use UTF-8, please amend your answer accordingly. Recommending the use of iso-8859-8 really is a bad idea. – einpoklum Mar 11 '17 at 22:52