Hi,
For some reason std::to_string
doesn’t work on Android Compilation, it simply states that to_string
is not member of std
. I am using cocos2d 3.10
Solution is to use StringUtils::toString
.
or you can use:
char buffer[5];
sprintf(buffer, “%d”, someint);
Related Solution
Or just create a header file like this, and include it:
#ifndef Templates_h
#define Templates_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 // Templates_h
I love how this keeps coming up.
As I recall switching APP_STL from gnu_static
to c++_static
was the agreed solution, but I don’t think a date was ever planned. Not sure about compatibility issues, maybe it should wait until 4.0?
@slackmoehrle can you please find out when/if this change is going to happen?
Relevant topic here: Why gnustl_static?
cocos2d and sdkbox compile and work just fine with c++_static
Yes c++_static
compiles fine but it isn’t the default, so new users get confused and waste time. Not sure if changing the default will cause problems for pre-existing projects, which is why I asked about waiting until 4.0
My biggest annoyance is the pre-compiled library packaged with Cocos doesn’t use c++_static
. I tried compiling them myself with c++_static
and gen-libs
but couldn’t get it work.
I am seeking assistance with this.
cpp project links c++_static is fine. However, js-binding and lua-binding have a problem.
js-binding problem
spidermonkey prebuilt binary(libjs_static.a) links libstdc++.
For supporting libc++, it must provide two prebuilt binaries that is linking libstdc++ and linking libc++, such as “Play Game Services for C++” SDK.
lua-binding problem
At c++_static compiles, link error luajit is failed because of Android NDK bug
(details https://code.google.com/p/android/issues/detail?id=73907 ).
this error is not still fixed on r10e. It must wait until releasing bugfix Android NDK version.
I just use this:
std::string to_str(int t) {
std::stringstream ss;
ss << t;
std::string str = ss.str();
return str;
}
std::string to_str(float t) {
std::stringstream ss;
ss << t;
std::string str = ss.str();
return str;
}
std::string to_str(long t) {
std::stringstream ss;
ss << t;
std::string str = ss.str();
return str;
}
std::string to_str(double t) {
std::stringstream ss;
ss << t;
std::string str = ss.str();
return str;
}
int from_str_to_int(std::string t){
const char* buffer = t.c_str();
int i = atoi(buffer);
return i;
}
long from_str_to_long(std::string t){
const char* buffer = t.c_str();
long i = atol(buffer);
return i;
}
double from_str_to_double(std::string t){
const char* buffer = t.c_str();
double i = atof(buffer);
return i;
}
float from_str_to_float(std::string t){
const char* buffer = t.c_str();
float i = atof(buffer);
return i;
}
@piotrros I know some people don’t like templates or won’t use them, but that 99% duplicate code is one place where it’s a no brainer I would think.
I’m using code below, and it should work.
template <typename T>
std::string to_string(T value)
{
std::ostringstream os ;
os << value ;
return os.str() ;
}
method to_string will be called depend on method above.
I know that! I wanted to use template, but had some issues on ms compiler if I remember correctly.