CentOS7 G ++“ to_string不在std的成员中”

时间:2020-02-03 20:57:52

标签: c++ gcc g++

我有一些非常标准的C ++代码,其中我使用to_string来添加字符串和整数,就像这样:

#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 = "\033[38;2"+std::to_string(red)+";"+std::to_string(green)+";"+std::to_string(blue)+";177m"+"\n"+text+"\n"+"\033[0m";
        return styled;
    }

};

当我尝试在Centos7上使用g ++进行编译时,出现错误“ to_string不在std的成员中”。我需要做什么?为什么这不起作用?我正在运行构建命令g++ main.cpp

我正在运行gcc版本4.8.5

1 个答案:

答案 0 :(得分:3)

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

在旧版GCC中,您可以将-std=c++0x用于C ++ 11或-std=c++1y用于C ++ 14。

相关问题