Reputation: 21
Having some trouble in this block of code at the TextOut line it says:
error: cannot convert 'std::string* {aka std::basic_string<char>*}' to
'LPCSTR {aka const char*}' for argument 4 to
'BOOL TextOutA(HDC, int, int, LPCSTR, int)'
I've searched for a while and wasn't able to find anything that didn't either eternally confuse me or would accomplish what I want to do which is draw a string.
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
string text = "Something";
RECT rect;
GetClientRect( hwnd, &rect );
hdc = BeginPaint( hwnd, &ps );
TextOut( hdc, rect.right/2, rect.bottom/2, &text, 1 );
EndPaint( hwnd, &ps );
}
return 0;
break;
Upvotes: 0
Views: 2942
Reputation: 8145
The error message explains what the problem is:
The LPCSTR
type is a typedef (alias for) const char *
. This is a C-style pointer, which is what the constant string "Something"
defaults to (but not text
).
The std::string
type is a typedef (alias for) std::basic_string<char>
. This is a C++ template class that is used to manage strings dynamically, like C#/Java strings, or the CString
type from MFC/ATL.
The &text
line is a pointer to the string object, not a pointer to the string itself. Likewise, std::string
does not provide implicit conversion to const char *
. You need to explicitly call text.c_str()
which is intended for use with APIs that take C-style strings.
For the last argument of TextOut
, you can pass -1
to get it to calculate the length of the string (not 1
). Alternatively, as you have the string in a std::string
object, you can use text.size()
.
Upvotes: 4
Reputation: 15501
You need to pass a pointer to a char array, not a C++ string. Try:
TextOut( hdc, rect.right/2, rect.bottom/2, text.c_str(), 1 );
Note that you've requested it to output only one character.
Upvotes: 3