简体   繁体   English

为什么这个程序会报错:to_string 不是 std 的成员。 为什么?

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

I am using VS2008 and Win7 64bit.我使用的是 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;
}

Error Messages.错误消息。

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 ==========

Those errors could mean:这些错误可能意味着:

  1. std::to_string requires header, which you did not include (but it doesn't) std::to_string 需要标头,您没有包括(但它没有)
  2. You didn't enable C++11 (I don't know how this works for Visual Studio)你没有启用 C++11(我不知道这对 Visual Studio 是如何工作的)
  3. You compiler does not support C++11.您的编译器不支持 C++11。

From comments it seems that Visual Studio you are using does not support C++11, so you can use old good string stream and with template you can create an equivalent of std::to_string:从评论看来,您正在使用的 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));

Note that to use this function type T needs to have operator<< defined, but it's not a problem with types, which std::to_string supports.请注意,要使用此函数,类型T需要定义operator<< ,但这对于 std::to_string 支持的类型不是问题。

Apart from the problem here of the compiler not supporting C++11 , there is another reason for this error which I stumbled on.除了这里的编译器不支持C++11的问题之外,我偶然发现了这个错误的另一个原因。

If you're using gcc on Ubuntu, you can use #include <string.h> and std::to_string will work.如果您在 Ubuntu 上使用gcc ,您可以使用#include <string.h>并且std::to_string将起作用。 But this fails with the MSVC compiler from Visual Studio 2019 because <string.h> is an older C header.但这在Visual Studio 2019MSVC编译器中失败,因为<string.h>是较旧的 C 头文件。 To resolve, use <string> instead.要解决,请改用<string> This is a difference with the interfaces with the C99 and C++11 standards wbetweenth the gcc and MSVC compilers.这与gccMSVC编译器之间的 C99 和 C++11 标准接口不同。

Source was this related question .来源是这个相关问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM