【问题标题】:Why is this program giving an error: to_string is not a member of std. Why?为什么这个程序会报错:to_string 不是 std 的成员。为什么?
【发布时间】:2015-08-21 12:10:10
【问题描述】:

我使用的是 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:\Debug\BuildLog.htm"
1>RasterizationLineCircleEllipse - 15 error(s), 14 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

【问题讨论】:

  • 那个版本支持c++11吗?
  • 可能的相关问题 - 1 2
  • Visual Studio 2008 不支持 C+11,考虑到它已于 2008 年发布,这会很奇怪。
  • 哦,在下一个问题之前,请参阅stackoverflow.com/questions/20302891/…
  • 对于您似乎需要查看代码的内容(将各种数据连接到字符串中),请使用std::ostringstream

标签: c++ compiler-errors bgi


【解决方案1】:

这些错误可能意味着:

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

从 cmets 看来,您使用的 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&lt;&lt;,但这不是 std::to_string 支持的类型的问题。

【讨论】:

    【解决方案2】:

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

    如果您在 Ubuntu 上使用 gcc,则可以使用 #include &lt;string.h&gt;std::to_string 将起作用。但是使用来自Visual Studio 2019MSVC 编译器会失败,因为&lt;string.h&gt; 是较旧的C 头文件。要解决此问题,请改用 &lt;string&gt;。这是在 gccMSVC 编译器之间实现 C99 和 C++11 标准的区别。

    来源是this related question

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 1970-01-01
      • 2014-02-23
      • 2014-11-23
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2020-05-23
      相关资源
      最近更新 更多