"to_string"不是"std"的成员?
"to_string" isn't a member of "std"?
好的,所以我有
TMP.cpp:
#include <string>
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( 上。
知道为什么会这样吗?这是标准行为,我做错了什么还是某处有错误?
您可能
希望指定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
。
to_string适用于
最新的C++版本,如版本 11。对于旧版本,您可以尝试使用此功能
#include <string>
#include <sstream>
template <typename T>
std::string ToString(T val)
{
std::stringstream stream;
stream << val;
return stream.str();
}
通过添加模板,您也可以使用任何数据类型。您必须在此处包含#include<sstream>
。
相关文章:
- std::ofstream 作为类成员删除复制构造函数?
- 使用 std::index_sequence 初始化具有固定大小数组成员的 POD 结构容器
- std::to_string - 'to_string' 不是 'std' 的成员 - Visual Studio Code 1.42.0
- 使用 std::async 时死锁,将来作为成员
- 访问 std:vector 的类成员 std:vector 在一个类中与另一个 std:vector
- 将此类的智能指针追加到成员 std::vector
- 在Visual Studio中删除成员std::vector::d ata()
- C++初始化/填充静态成员 std::数组元素到特定值
- 是否可以在同一对象中对成员 std::shared_ptr 进行成员引用<variable>?
- 成员std::future防止boost::shared_ptr超出范围
- 在GCC中使用Brace Initializer初始化成员STD ::数组时错误
- 错误:非静态引用成员"std::ostream&Student::out",无法使用默认赋值运算符
- 获取指向成员std::string::size的指针无法与libc++链接,但可以与libstdc++一起使用
- 如何创建静态常量成员 std::string 数组
- 如何在构造函数初始值设定项列表中初始化“std::unique_ptr”对象的类成员“std::vector”
- C++ 编译失败,并显示错误:命名空间中没有名为 'snprintf' 的成员'std'
- 重载类下标运算符以访问成员std::vector对象的元素
- 构造类成员 std::vector<std::unique_ptr > 的智能方法<AClass>
- 使用成员std::vector移动语义
- 初始化类成员std::函数时编译错误