Error : 'to_wstring' is not a member of 'std'

Error : 'to_wstring' is not a member of 'std'

我不知道为什么这不能编译。

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

编译器:gcc 5.1.0

IDE : 代码块 17.12

您必须确保:

您已包含字符串 header:

#include <string>

您正在使用 c++11 标志进行编译:-std=c++11

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

这是官方文档 https://en.cppreference.com/w/cpp/string/basic_string/to_wstring

----当然,我希望你的意思是

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

而不是

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

因为你不能声明 number 并用另一个名为 number...

的变量初始化它

此示例 here 工作正常:

#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;
}

解决方法

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