一、问题描述:
想跑一段代码,结果遇到error: ‘to_string’ is not a member of ‘std’
二、解决方法
仔细看了几遍代码,感觉大概率没有错误,于是查了下资料
(1)Cmakelists.txt 需要加上
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions(-D_GLIBCXX_USE_C99=1)
试了试还是没办法解决这个问题
(2)后来看到这个方法:可以自己写一个 std::to_string()
替换标准库中的,写一个stdtostring.h
文件。
#ifndef STDTOSTRING_H
#define STDTOSTRING_H
#include <string>
#include <sstream>
using namespace std;
namespace std
{
template < typename T > std::string to_string( const T& n )
{
std::ostringstream stm ;
stm << n ;
return stm.str() ;
}
}
#endif
在需要用到 std::to_stirng()
方法的源文件加上
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "stdtostring.h"
#endif
就解决了
三、尝试结果
问题解决,没有编译出错,listener也接收到了talker发布的信息
四、补充注意
(1)编译时候要在工作空间workspace下(我这里是catkin_ws)编译,不能在src下
(2)编译结束后需要刷新一下环境再运行,每开一个终端也要重新刷新一下环境:
source devel/setup.bash
五、参考:
(12条消息) error: ‘to_string’ is not a member of ‘std’———已解决_Anzhongliu的专栏-CSDN博客