Centos7 g++ "to_string is not in a member of std"

Centos7 g++ "to_string is not in a member of std"

我有一些非常标准的 C++ 代码,我在其中使用 to_string 将 sting 和 int 添加到一起,如下所示:

#include <string>
using namespace std;

class Color{
public:
    int red, blue, green;

    Color(int r, int g, int b){
        red = r;
        green = g;
        blue = b;
    }

    string to_string(){
        string color = "(" + std::to_string(red) + ", " + std::to_string(green) + ", " + std::to_string(blue) + ")";
        return color;
    }

    string colorize(string text){
        string styled = "3[38;2"+std::to_string(red)+";"+std::to_string(green)+";"+std::to_string(blue)+";177m"+"\n"+text+"\n"+"3[0m";
        return styled;
    }

};

当我尝试在 Centos7 上使用 g++ 编译它时,出现错误 "to_string is not in a member of std"。我需要做什么?为什么这不起作用?我是 运行 构建命令 g++ main.cpp

我是 运行 gcc 版本 4.8.5

我相信你缺少的是编译器标志 -std=c++11 因为 std::to_string 是在标准的 C++11 版本中引入的。

在旧版本的 GCC 中,您可以对 C++11 使用 -std=c++0x 或对 C++14 使用 -std=c++1y