C++ Use of wstring_convert on Linux

3.9k Views Asked by At

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?

2

There are 2 best solutions below

4
On BEST ANSWER

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

0
On

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: