error: ‘to_string‘ is not a member of ‘std‘ 或 error: ‘thread‘ is not a member of ‘std‘ 原因及解决办法

简介: error: ‘to_string‘ is not a member of ‘std‘ 或 error: ‘thread‘ is not a member of ‘std‘ 原因及解决办法

一、错误描述

昨天在编译 paho.mqtt.cpp-1.0.1 时出错,提示 error: 'to_string' is not a member of 'std' ,

在网上看了一下,类似的错误还有:

error: 'to_string' is not a member of 'std'
error: 'thread' is not a member of 'std'

二、原因分析

因为 to_string 和 thread 是 C++11 才支持的,所以出现这种错误的原因可能是编译时漏了支持 C++11 的编译选项。


三、解决办法

3.1 一般情况在编译选项加上 -std=c++11 或 -std=gnu++11。

3.2 如果是使用 cmake 生成 Makefile 的,需要检查 CMakeLists.txt 文件的 C++11配置,加上如下几句代码

## --- C++11 build flags ---
add_definitions(-D_GLIBCXX_USE_C99=1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)


目录
相关文章
|
2月前
|
存储 安全 API
C++ 17 新特性 C++ String View:了解C++ 17 std::string_view的使用场景
C++ 17 新特性 C++ String View:了解C++ 17 std::string_view的使用场景
125 2
|
12天前
|
安全 C++
C++一分钟之-字符串处理:std::string
【6月更文挑战第25天】`std::string`是C++文本处理的核心,存在于`<string>`库中。它支持初始化、访问、连接、查找、替换等操作。常见问题包括空指针解引用、越界访问和不当内存管理。要安全使用,确保字符串初始化,用`at()`检查边界,用`.empty()`检查空字符串,且无需手动释放内存。高效技巧包括预先分配内存、利用互转函数以及使用迭代器。记得正确比较和遍历字符串以保证代码效率和安全性。
29 5
|
4天前
|
C++
【C++】string类的使用④(常量成员Member constants)
C++ `std::string` 的 `find_first_of`, `find_last_of`, `find_first_not_of`, `find_last_not_of` 函数分别用于从不同方向查找目标字符或子串。它们都返回匹配位置,未找到则返回 `npos`。`substr` 用于提取子字符串,`compare` 则提供更灵活的字符串比较。`npos` 是一个表示最大值的常量,用于标记未找到匹配的情况。示例代码展示了这些函数的实际应用,如替换元音、分割路径、查找非字母字符等。
|
1月前
|
存储 程序员 C++
stoll函数和std::to_string函数
stoll函数和std::to_string函数
13 0
|
2月前
|
C++
C++ std::string类的使用
C++ std::string类的使用
34 0
|
2月前
|
存储 安全 前端开发
Java中的String类与StringBuilder、StringBuffer的比较:缺点与解决办法
Java中的String类与StringBuilder、StringBuffer的比较:缺点与解决办法
66 0
|
8月前
|
编译器
error TS2322 Type ‘string null‘ is not assignable to type ‘string undefined‘.
error TS2322 Type ‘string null‘ is not assignable to type ‘string undefined‘.
|
9月前
|
C++
std::string 不能跨dll的一种解决方法
std::string 不能跨dll的一种解决方法
|
9月前
|
C++
MFC exe使用C++ dll中的std::string 崩溃
MFC exe使用C++ dll中的std::string 崩溃
VC7(VS2002)调试时 std::string 超过15字符乱码问题
VC7(VS2002)调试时 std::string 超过15字符乱码问题