to_string() not working for Android.What to use instead for cross-platform Game?

to_string() not working for Android.What to use instead for cross-platform Game ?

I used this when I ran into the same problem…

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
template<typename T>
string to_string(const T& t) {
  ostringstream os;
  os << t;
  return os.str();
}
#endif

undefined symbol “ostringstream”…

and i need to add this to my Constants/Utility Class i think…am i right ?

you need to include

http://www.cplusplus.com/reference/sstream/ostringstream/

int iCount;
iCount=140;
String::createWithFormat("%d",iCount);

jni/…/…/Classes/Constants.h:37:24: error: ‘T’ does not name a type
string to_string(const T& t) {
^
jni/…/…/Classes/Constants.h:37:27: error: ISO C++ forbids declaration of ‘t’ with no type [-fpermissive]
string to_string(const T& t) {
^
jni/…/…/Classes/Constants.h:37:28: error: ‘to_string’ is not a template function
string to_string(const T& t) {
^
jni/…/…/Classes/Constants.h:37:30: error: expected ‘;’ before ‘{’ token
string to_string(const T& t) {
^

After using your code @IslandPlaya

@sayedtdm you mean to say CCString::createWithFormat ?

@rammehta

cocos2d-x version 2.x

CCString::createWithFormat

cocos2d-x version 3.x its just

String::createWithFormat

k…it should be like this :
std::string(String::createWithFormat("")->getCString());

Also i get a line on String
i think it means deprecated…

You are sort of mixing the STL and Cocos.

So you are trying to take an int and create a string representation out of it?

int iCount = 140;
std::string s = std::to_string(iCount);

std::string z = s.c_str() // get cstring

@slackmoehrle
Well the question was “to_string” doesnt work for android !
and your solution has “to_string” …
even above method to implement my own “to_string” failed…

Apology’s! Should have checked my post… Seems like the forum garbled it for some reason…
It should be this:-

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
template<typename T>
string to_string(const T& t) {
	ostringstream os;
	os << t;
	return os.str();
}
#endif
1 Like

BINGO !!!
Thanks mate !!!

Where do I have to add this code? Please, help me.

Just add it into a header file that is included in any source file that needs it.

Just use the new version of NDK.

1 Like

I’m confused, std::to_string does work for Android. Which version of the NDK were you using?

http://en.cppreference.com/w/cpp/string/basic_string/to_string

It didn’t work on older versions of the NDK…

I get this problem even with r10e. Which version of NDK was it working for you ?

it ommits the error below
jni/…/…/Classes/HelloWorldScene.cpp:1359: error: undefined reference to ‘HelloWorld::stoi(std::string)’