0

I am using VS2008 and 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;
}

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 ==========
genpfault
  • 51,148
  • 11
  • 85
  • 139
user366312
  • 16,949
  • 65
  • 235
  • 452
  • 1
    does that version support c++11? – Nim Aug 21 '15 at 12:11
  • Possible relevant quesitons - [1](http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-so-g) [2](http://stackoverflow.com/questions/19122574/to-string-isnt-a-member-of-std) – Bernhard Barker Aug 21 '15 at 12:12
  • @Dukeling, not sure either of those are relevant (different compiler), the main problem stems from VS2008 being quite old and having no support for C++11! – Nim Aug 21 '15 at 12:13
  • 6
    Visual Studio 2008 does not support C+11, which would be strange given that it has been released in 2008. – Banex Aug 21 '15 at 12:14
  • 1
    Oh, and before the next question, see http://stackoverflow.com/questions/20302891/visual-studio-2008-with-c11 – Nim Aug 21 '15 at 12:14
  • I just need to convert integer to string. – user366312 Aug 21 '15 at 12:15
  • @anonymous [http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c]This should then answer your question – mkaes Aug 21 '15 at 12:18
  • 1
    For what you seem to need looking at the code (concatenate various data into the string), use [`std::ostringstream`](http://www.cplusplus.com/reference/sstream/ostringstream/). – bereal Aug 21 '15 at 12:21

2 Answers2

8

Those errors could mean:

  1. std::to_string requires header, which you did not include (but it doesn't)
  2. You didn't enable C++11 (I don't know how this works for Visual Studio)
  3. You compiler does not support 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:

#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.

zoska
  • 1,684
  • 11
  • 23
4

Apart from the problem here of the compiler not supporting C++11, there is another reason for this error which I stumbled on.

If you're using gcc on Ubuntu, you can use #include <string.h> and std::to_string will work. But this fails with the MSVC compiler from Visual Studio 2019 because <string.h> is an older C header. To resolve, use <string> instead. This is a difference with implementation of the C99 and C++11 standards between the gcc and MSVC compilers.

Source was this related question.

AlainD
  • 5,413
  • 6
  • 45
  • 99