PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

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

 June 25, 2022     bgi, c++, compiler-errors     No comments   

Issue

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

Solution

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.



Answered By - zoska
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing