0

I would like to be able to convert text read from a file into multibyte characters. I have the following C++ code on Windows that is working for me. When I try to compile the code on Linux though it is failing.

#include <locale>
....
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utfconv;
std::string line;
while (std::getline(infile, line))
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utfconv;
    std::wstring widestr = utfconv.from_bytes(line);

This throws the following errors:

wstring_convert is not a member of std
codecvt_utf8_utf16 is not a member of std
expected primary-expression before wchar_t

I'm using GCC Red Hat 4.4.7-4. Based on what I read, I've imported 'locale', but it still can't find it.

If wstring_convert is not available, is there something equivalent that I can do?

George Hernando
  • 2,550
  • 7
  • 41
  • 61
  • You might want to use `std::u16string` instead of `std::wstring` if your compilers have access to it. I think you can use `std::wstring_convert` with `char16_t` strings. The reason is that `wchar_t` has an implementation-defined size, whereas `char16_t` is specifically for holding UTF-16 characters. – James Picone Jun 15 '18 at 04:04

2 Answers2

3

Most likely your standard is not set properly. std::wstring_convert was first introduced in C++11 and deprecated in C++17, so you need to add the compiler flag -std=c++11 or -std=c++14.

EDIT: Just saw the GCC version you're using. It's way out of date. Everything should work fine if you download GCC 4.9.x or above

1

You will have to use a newer GCC version. Precompiled versions are part of Developer Toolset (available as part of most Red Hat Enterprise Linux subscriptions) or as part of Software Collections for CentOS:

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92