我是靠谱客的博主 炙热野狼,这篇文章主要介绍[Error] 'to_string' was not declared in this scope to_string()用不了解决方法:,现在分享给大家,希望可以做个参考。
在C++11标准库中,string.h已经添加了to_string方法,方便从其他类型(如整形)快速转换成字面值。但是在NDK编译过程中发现如下问题:
error: 'to_string' was not declared in this scope
使用std::to_string()之后继续报错
error: 'to_string' is not a member of 'std'
解决方法:
1.自己写一个函数
因为to_string这个函数只有C++11才有,这个报错应该是在旧版本C++使用该函数的原因,如果不能使用需要自己写一个函数:
在流文件下,需要使用“sstream”头文件:
template <typename T>
std::string to_string(T value)
{
std::ostringstream os ;
os << value ;
return os.str() ;
}
程序代码:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
template <typename T>
std::string to_string(T value)
{
std::ostringstream os ;
os << value ;
return os.str() ;
}
int main(){
int lcc1 = 1;
long lcc2 = 122;
double lcc3 = 2.1;
string s1 = to_string(lcc1);
string s2 = to_string(lcc2);
string s3 = to_string(lcc3);
cout<<s1<<" "<<s2<<" "<<s3<<endl;
}
运行结果:

2.查看Application.mk中,模板库选择,发现
APP_STL := gnustl_static
改成
APP_STL := c++_static
编译通过!
最后
以上就是炙热野狼最近收集整理的关于[Error] 'to_string' was not declared in this scope to_string()用不了解决方法:的全部内容,更多相关[Error]内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复