I am trying to make a variable with the data type std::map<char, wchar_t>
in C . When I try this, Visual Studio gives this build error:
C2440 'initializing': cannot convert from 'initializer list' to 'std::map<char,wchar_t,std::less<char>,std::allocator<std::pair<const char,wchar_t>>>'
The same error also occurs when wchar_t
is the key data type and char
is the value data type.
You can replicate this error by running the following code:
const std::map<char, wchar_t> UNICODE_MAP = {
{ 'X', L"█" },
{ 'G', L"▓" },
{ 'O', L"ᗣ" },
{ 'P', L"ᗤ" }
};
How would I make a map with the key as a char
and the value as a wchar_t
data type?
CodePudding user response:
It's a map from narrow char to wide char, not char to wide string:
Instead of this:
const std::map<char, wchar_t> UNICODE_MAP = {
{ 'X', L"█" },
{ 'G', L"▓" },
{ 'O', L"ᗣ" },
{ 'P', L"ᗤ" }
};
Use this:
const std::map<char, wchar_t> UNICODE_MAP = {
{ 'X', L'█' },
{ 'G', L'▓' },
{ 'O', L'ᗣ' },
{ 'P', L'ᗤ' }
};
If you actually need the value to be a string, then declare it as:
const std::map<char, std::wstring> UNICODE_MAP = {
{ 'X', L"█" },
...
Also, while I'm here, I advocate for escaping all Unicode chars in source files that are not in the ASCII range. Between IDEs with varying behaviors, revision control systems, and the guy down the hall who didn't read your memo about "save as Unicode", this can get easily corrupted. So... my final recommendation is this:
const std::map<char, wchar_t> UNICODE_MAP = {
{ 'X', L'\u2588' },
{ 'G', L'\u2593' },
{ 'O', L'\u15e3' },
{ 'P', L'\u15e4' }
};