为什么这个程序给出一个错误:to_string不是std的成员

Why is this program giving an error: to_string is not a member of std. Why?

本文关键字:to string std 成员 错误 不是 一个 程序 为什么      更新时间:2023-10-16

我使用的是VS2008和Win7 64bit。

#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:DebugBuildLog.htm"
1>RasterizationLineCircleEllipse - 15 error(s), 14 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

这些错误可能意味着:

  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));

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

除了编译器不支持C++11的问题,还有另一个原因,我无意中发现了这个错误。

如果你在Ubuntu上使用gcc,你可以使用#include <string.h>std::to_string将工作。但这在Visual Studio 2019MSVC编译器中失败了,因为<string.h>是一个较旧的C头文件。要解决这个问题,请使用<string>。这是gccMSVC编译器之间C99和c++ 11标准实现的不同之处。

来源是这个相关的问题