[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

编译通过!
 

发布了303 篇原创文章 · 获赞 550 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_42410605/article/details/103092876