error: ‘to_string’ is not a member of ‘std’———已解决
現象?
cocos2d-x 2.2.6 項目的源碼中使用了?std::to_string()?方法,使用 NDK r9d 編譯的時候,報如下錯誤:
error: 'to_string' is not a member of 'std'Application.mk 文件的部分內容如下:
APP_STL := gnustl_static NDK_TOOLCHAIN_VERSION := 4.8 APP_CPPFLAGS := -frtti -std=c++11 -fexceptions -Wno-error=format-security -Wno-literal-suffix -Wno-deprecated-declarations -fsigned-char -Os $(CPPFLAGS)換用 NDK 10e ,錯誤依然。
快速解決?
想快速解決這個問題,可以自己寫一個?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追根溯源?
Why gnustl_static?中介紹了采用?c++_static?和?clang?編譯器來實現std::to_string()?支持,于是我嘗試了下:
# APP_STL := gnustl_static # NDK_TOOLCHAIN_VERSION := 4.8 APP_STL := c++_static NDK_TOOLCHAIN_VERSION := clang根據?C++ Library Support?的說明,?c++_static?對應的是 The LLVM libc++ runtime (static) ,因此應該采用 clang 的工具鏈進行編譯。
結果是:?std::to_string()?編譯正常,但出現下面的錯誤:
In file included from src/Utilities.cpp:1:0: android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1498:46: error: expected unqualified-id before 'float' inline _LIBCPP_INLINE_VISIBILITY float remainder(float __x, float __y) _NOEXCEPT {return remainderf(__x, __y);}^ android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1498:46: error: expected ')' before 'float' android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1499:46: error: expected unqualified-id before 'long' inline _LIBCPP_INLINE_VISIBILITY long double remainder(long double __x, long double __y) _NOEXCEPT {return remainderl(__x, __y);}^ android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1499:46: error: expected ')' before 'long' android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1509:1: error: expected ')' before '__x' remainder(_A1 __x, _A2 __y) _NOEXCEPT ^ android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1559:46: error: expected unqualified-id before 'float' inline _LIBCPP_INLINE_VISIBILITY float round(float __x) _NOEXCEPT {return roundf(__x);}^ android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1559:46: error: expected ')' before 'float' android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1560:46: error: expected unqualified-id before 'long' inline _LIBCPP_INLINE_VISIBILITY long double round(long double __x) _NOEXCEPT {return roundl(__x);}^ android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1560:46: error: expected ')' before 'long' android-ndk-r9d/sources/cxx-stl/llvm-libc++/libcxx/include/cmath:1565:1: error: expected ')' before '__x' round(_A1 __x) _NOEXCEPT {return round((double)__x);}minggo?在?Cocos can’t compile Android with APP_STL := c++_static?中提到:
A prebuilt version can only support one type of c++ lib. Did you mean we should provide two kinds of prebuilt libcocos2d that using two different c++ libs?
I don’t know if cocos2d can compile with c++_static or not. I don’t remember if there is a good reason to choose one instead of other one. But cocos2d-x uses gnustl_static from very early version, so we know it works well. You can replace it with c++_static if you like, but it is not fully tested.
因此我懷疑在 cocos2d-x 的舊版本中,?c++_static?是沒有經過完整測試的。
也有人說?在新版本(>3.7)中,?gnustl_static?會導致 lambda 和std::to_string?不能使用,已經切換到使用?c++_static?和 clange 組合。
I never made it to work with gnustl_static and gcc with all the c++11 features. I was getting crashes in simple lambdas, as mentioned std::to_string was missing and much more, so i’ve switched to c++_static and clang long time ago.
本文轉自:http://www.tuicool.com/articles/RBBjMz
轉載于:https://www.cnblogs.com/Anzhongliu/p/6091789.html
總結
以上是生活随笔為你收集整理的error: ‘to_string’ is not a member of ‘std’———已解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创建一个catkin工作空间
- 下一篇: js时间戳格式化成日期格式