我正在使用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;
}
错误讯息。
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 ==========
答案 0 :(得分:4)
这些错误可能意味着:
从评论看来,您使用的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支持的类型问题。
答案 1 :(得分:0)
除了这里的编译器不支持C++11
的问题之外,还有我偶然发现的该错误的另一个原因。
如果您在Ubuntu上使用gcc
,则可以使用#include <string.h>
,而std::to_string
将起作用。但这对于MSVC
的{{1}}编译器来说是失败的,因为Visual Studio 2019
是较旧的C标头。若要解决,请改用<string.h>
。这与<string>
和gcc
编译器之间具有C99和C ++ 11标准的接口有所不同。