Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » urgh Conversion...

This thread is locked; no one can reply to it. rss feed Print
urgh Conversion...
xmltorrent
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:

Quote:

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:

Quote:

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.

CursedTyrant
Member #7,080
April 2006
avatar

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.

---------
Signature.
----
[My Website] | [My YouTube Channel]

OICW
Member #4,069
November 2003
avatar

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 a.cc. 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: