为什么这个程序发出错误:to_string不是std的成员。为什么?

时间:2015-08-21 12:10:11

标签: c++ compiler-errors

我正在使用VS2008和Win7 64位。

#include <iostream>
#include "utils.h"
#include <string>

int main()
{
    int gm = DETECT;
    int gd = DETECT;
    initgraph(&gm, &gd, "");
    double x = 0;
    double y = 0;
    double r = 50;
    for(int i=0 ; i<=360 ; i++)
    {
        x = r * cos(DegreeToRad((double)i));
        y = r * sin(DegreeToRad((double)i));

        PlotLine(0,0,x,y, YELLOW);

        std::string str;
        str.append(std::to_string(i));
        str.append(". ");
        str.append(std::to_string(0));
        str.append(", ");
        str.append(std::to_string(0));
        str.append(") to (");
        str.append(std::to_string(x));
        str.append(", ");
        str.append(std::to_string(y));
        str.append(").  m=");
        str.append(std::to_string(Slope(0,0,x,y)));
        str.append("\n");       

        if(i%90==0)
        {
            str.append("\ni=");
            str.append(std::to_string(i));
            str.append("\n");
        }

        WriteToFile("slope.txt", str.c_str());
    }

    getch();
    closegraph();

    return 0;
}

错误讯息。

1>e:\slope.test.cpp(21) : error C2039: 'to_string' : is not a member of 'std'
1>e:\slope.test.cpp(21) : error C3861: 'to_string': identifier not found
1>e:\slope.test.cpp(23) : error C2039: 'to_string' : is not a member of 'std'
1>e:\slope.test.cpp(23) : error C3861: 'to_string': identifier not found
1>e:\slope.test.cpp(25) : error C2039: 'to_string' : is not a member of 'std'
1>e:\slope.test.cpp(25) : error C3861: 'to_string': identifier not found
1>e:\slope.test.cpp(27) : error C2039: 'to_string' : is not a member of 'std'
1>e:\slope.test.cpp(27) : error C3861: 'to_string': identifier not found
1>e:\slope.test.cpp(29) : error C2039: 'to_string' : is not a member of 'std'
1>e:\slope.test.cpp(29) : error C3861: 'to_string': identifier not found
1>e:\slope.test.cpp(31) : error C2039: 'to_string' : is not a member of 'std'
1>e:\slope.test.cpp(31) : error C3861: 'to_string': identifier not found
1>e:\slope.test.cpp(37) : error C2039: 'to_string' : is not a member of 'std'
1>e:\slope.test.cpp(37) : error C3861: 'to_string': identifier not found
1>Generating Code...
1>Build log was saved at "file://e:\Debug\BuildLog.htm"
1>RasterizationLineCircleEllipse - 15 error(s), 14 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

2 个答案:

答案 0 :(得分:4)

这些错误可能意味着:

  1. std :: to_string需要标题,你没有包含(但它没有)
  2. 您没有启用C ++ 11(我不知道这对Visual Studio有何用处)
  3. 您的编译器不支持C ++ 11。
  4. 从评论看来,您使用的Visual Studio似乎不支持C ++ 11,因此您可以使用旧的良好字符串流,使用模板可以创建等效的std :: to_string:

    #include <sstream>
    #include <string>
    
    template<class T>
    std::string toString(const T &value) {
        std::ostringstream os;
        os << value;
        return os.str();
    }
    
    //Usage:
    std::string valueStr = toString(10);
    valueStr.append(toString(1));
    valueStr.append(toString(2.5));
    

    请注意,要使用此函数类型T需要定义operator<<,但这不是std :: to_string支持的类型问题。

答案 1 :(得分:0)

除了这里的编译器不支持C++11的问题之外,还有我偶然发现的该错误的另一个原因。

如果您在Ubuntu上使用gcc,则可以使用#include <string.h>,而std::to_string将起作用。但这对于MSVC的{​​{1}}编译器来说是失败的,因为Visual Studio 2019是较旧的C标头。若要解决,请改用<string.h>。这与<string>gcc编译器之间具有C99和C ++ 11标准的接口有所不同。

来源为this related question