Strings and const chars.

So, I'm working with cocos2d-x and there's a specific function used to print true type fonts to the screen. My problem is that I want to feed a string into the function, but it only takes a string of const char (Which I don't even know what that is. AFAIK strings are a string of chars, not const chars).

Here's a definition of the function :

 
CCLabelTTF::create(const char *string, const char *fontname, float *fontsize);


an example of it being used

 
CCLabelTTF *pCoord = CCLabelTTF::create("Coordinates", "Arial", 42.0);


What I want to use
1
2
3
std::string coord;
//stream a constantly changing float into coord ...
CCLabelTTF *pCoord = CCLabelTTF::create(coord, "Arial", 42.0);


What happens : Error : no instance of overloaded function "...CCLabelTTF::create" matches the argument list. Argument types are (std::string, const char [6], int)

Basically, What I need to know is how would I feed a constantly updating string into the 1st argument?
Use

CCLabelTTF *pCoord = CCLabelTTF::create(coord.c_str(), "Arial", 42.0);

Last edited on
Write it like so:
1
2
3
std::string coord;
//stream a constantly changing float into coord ...
CCLabelTTF *pCoord = CCLabelTTF::create(coord.c_str(), "Arial", 42.0); // Note: c_str() 


c_str() returns a const char *. See:

http://www.cplusplus.com/reference/string/string/c_str/


AFAIK strings are a string of chars, not const chars).
That doesn't matter. Adding const is possible at any time.
Also, you're attempting to pass in a float as the third argument, when the definition specifies a pointer to a float.
Topic archived. No new replies allowed.