如何在 C++ 中 cout wstring 或 wchar_t
问题:
你有一个 std::wstring 想使用 cout 打印
test-wstring.cpp
wstring w = L"Test: äöü";
cout << w << endl;但你看到类似这样结尾的长错误消息:
compile-error.txt
/usr/include/c++/7/ostream:682:5: note: template argument deduction/substitution failed:
/usr/include/c++/7/ostream: In substitution of 'template<class _Ostream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable<typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type, const _Tp&, void> >::value, typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type>::type std::operator<<(_Ostream&&, const _Tp&) [with _Ostream = std::basic_ostream<char>&; _Tp = std::__cxx11::basic_string<wchar_t>]':
test.cpp:9:13: required from here
/usr/include/c++/7/ostream:682:5: error: no type named 'type' in 'struct std::enable_if<false, std::basic_ostream<char>&>'解决方案
你需要使用 std::wcout 而不是 std::cout:
print-wstring.cpp
wstring w = L"Test: äöü";
wcout << w << endl;Check out similar posts by category:
C/C++
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow