I use MFC TextOut to put some text on screen as follows
std::string myIntToStr(int number)
{
std::stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
void MViewClass::DrawFunction()
{
CClientDC aDC(this);
// .. Drawing Code
aDC.TextOut(27, 50, ("my age is " + myIntToStr(23)).c_str());
}
But I get error saying " cannot convert argument 3 from 'const char *' to 'const CString &'".
The documentation for TextOut shows a CString overload. I would like to use CString with TextOut as it allows me to use my myIntToStr converter. Any suggestions?
I assume that you use function
myIntToStrto convert an int to a string elsewhere in you code, and that you current problem is how to display a C++stringwith TextOut.You could simply create a CString in the stack initialized from the
std::stringthat way :As it is created on the stack, it will automatically vanish at the end of the method and you have not worry about allocation and deallocation.