2

I don't know why this is not compiling.

std::wstring number = std::to_wstring((long long)another_number);

Compiler : gcc 5.1.0

IDE : codeblocks 17.12

phuclv
  • 37,963
  • 15
  • 156
  • 475

2 Answers2

3

you have to ensure that:

you have included the string header:

#include <string>

you are compiling with c++11 flag: -std=c++11

$ g++ -std=c++11 your_file.cpp -o your_program

here is the official doc https://en.cppreference.com/w/cpp/string/basic_string/to_wstring

----and ofcourse, i hope you mean something like

 std::wstring number = std::to_wstring((long long)anotherNumber);

instead of

std::wstring number = std::to_wstring((long long)number);

coz you cant declare number and initialize it with a another variable named number...

this example here is working fine:

#include <iostream>
#include <string>

int main() {
    auto numberX{2020};
    std::wstring f_str = std::to_wstring((long long)numberX);
    std::wcout << f_str;
    
    return 0;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • In GCC 5.4.0 [the default standard version is `-std=gnu++98`](https://stackoverflow.com/a/44057210/995714) so I guess it's really the reason – phuclv Jun 24 '21 at 08:35
0

Workaround

std::string temp = std::to_string((long long)number);
std::wstring number_w(temp.begin(), temp.end());