为什么这个程序给出一个错误:to_string不是std的成员
Why is this program giving an error: to_string is not a member of std. Why?
我使用的是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:DebugBuildLog.htm"
1>RasterizationLineCircleEllipse - 15 error(s), 14 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
这些错误可能意味着:
- std::to_string需要头,你没有包含(但它没有)
- 你没有启用c++ 11(我不知道这在Visual Studio中是如何工作的)
- 你的编译器不支持c++ 11。
从注释来看,你正在使用的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支持的类型来说,这不是问题。
除了编译器不支持C++11
的问题,还有另一个原因,我无意中发现了这个错误。
如果你在Ubuntu上使用gcc
,你可以使用#include <string.h>
和std::to_string
将工作。但这在Visual Studio 2019
的MSVC
编译器中失败了,因为<string.h>
是一个较旧的C头文件。要解决这个问题,请使用<string>
。这是gcc
和MSVC
编译器之间C99和c++ 11标准实现的不同之处。
来源是这个相关的问题
相关文章:
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- std::time_point from and to std::string
- "No suitable conversion function from 'std::string' to 'const char *' exists"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 使用词法强制转换在'string to double'中设置双精度变量的精度
- 在 Lua 中加载 C++ 模块时'Attempt to index a string value'错误
- std::string to std::regex
- "How to pass String value in between URL in C/C++?"
- "conversion from void to non scalar type string"是什么意思?
- 我收到一个" conversion from ‘std::string (*)[50]’ to non-scalar type ‘std::string’ requested"错误,似乎无法修复它
- 如何防止从函数生成的字符串中使用科学记数法"number to string"?
- hwProfileInfo.szHwProfileGuid to String?
- 编写我自己的字符串类"reference to ‘string’ is ambiguous"但出现编译错误
- c++ stringstream to ostream to string
- How cin to string?
- C++ *string convert to string
- Round Double and Cast to String
- Regex-ed value to String
- vector string to string (or c_string) to lpszString