为什么该程序给出错误:to_string不是std的成员。为什么?

use*_*312 -1 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;
}
Run Code Online (Sandbox Code Playgroud)

错误消息。

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 ==========
Run Code Online (Sandbox Code Playgroud)

zos*_*ska 5

这些错误可能意味着:

  1. std :: to_string需要标头,您没有包含标头(但没有标头)
  2. 您没有启用C ++ 11(我不知道这对于Visual Studio是如何工作的)
  3. 您的编译器不支持C ++ 11。

从注释看来,您使用的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));
Run Code Online (Sandbox Code Playgroud)

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