- Online Community Forums » Programming Questions » urgh Conversion...

This thread is locked; no one can reply to it. rss feed Print
urgh Conversion...
Member #7,673
August 2006

I'm using C++ for my latest game.

I'm having problems using the text output functions in Allegro with the std::string type used in C++.

For instance this following line of code:

textout_ex(screen, font, greg.getName(), 0, 0, makecol(255, 255, 255), -1);

Returns this to the compiler:


Invalid type in argument 3.

So I decided to cast types:

textout_ex(screen, font, static_cast<char>(greg.getName()), 0, 0, makecol(255, 255, 255), -1);

And this is what the comiler tells me:


Invaild cast from std::string to char

Sounds like a n00b question but I haven't messed with std::string in a while and I'm totally in the dark with it.

Member #7,080
April 2006

Assuming that GetName returns a string:

textout_ex(screen, font, greg.getName().c_str(), 0, 0, makecol(255, 255, 255), -1);

AFAIK you don't cast types using static or dynamic cast. Correct casting would look like this:

(char *)greg.getName()

... which in this case would still be incorrect.

Of course, I could be wrong, as I've only used dynamic_cast, and never static_cast.

[My Website] | [My YouTube Channel]

Member #4,069
November 2003

In that case you need a pointer, but that string is static, besides you cannot retype string to embeded type. As CursedTyrant said, you have to use string member function c_str() to make it work.

[My website][CppReference][Pixelate][Allegators worldwide][Who's online]
"Final Fantasy XIV, I feel that anything I could say will be repeating myself, so I'm just gonna express my feelings with a strangled noise from the back of my throat. Graaarghhhh..." - Yahtzee
"Uhm... this is Did you honestly think this thread WOULDN'T be derailed and ruined?" - BAF
"You can discuss it, you can dislike it, you can disagree with it, but that's all what you can do with it"

Go to: