Stack overflow地址:c++ - "to_string" isn't a member of "std"? - Stack Overflow
翻译:
我有tmp.cpp:
#include
int main()
{
std::to_string(0);
return 0;
}
但是当我想要编译的时候,我得到了:
$ g++ tmp.cpp -o tmp
tmp.cpp: In function ‘int main()’:
tmp.cpp:5:5: error: ‘to_string’ is not a member of ‘std’
std::to_string(0);
^
我使用的g++版本是 4.8.1。与其他发生次错误的链接不同,我没有使用MinGW,我在Linux(3.11.2)上。
有没有任何的想法关于这种情况的发生?这是标准的行为,我做错了什么或者有什么bug在这里?
Answers1:
你可以指定C++的版本:
g++ -std=c++11 tmp.cpp -o tmp
我手上没有gcc 4.8.1版本,但是在老的GCC版本中,你可以这么做:
g++ -std=c++0x tmp.cpp -o tmp
至少在GCC 4.9.2版本我相信已经支持C++14了,这么指定:
g++ -std=c++1y tmp.cpp -o tmp
更新:gcc 5.3.0现在(我正在使用的cygwin版本)已经支持 -std=C++14和 -std=C++17。
Answers2:
to_string工作在最近的C++版本像 C++11。对于来的版本你可以使用下面的函数:
#include
#include
template
std::string ToString(T val)
{
std::stringstream stream;
stream << val;
return stream.str();
}
通过添加模板你可以使用任意的数据类型。你需要包含 #include <sstream>在这里。