Convert from 'QChar' to 'wchar_t' - c++

I need to pass a QChar to a function that expects a wchar_t:
void myFunc(wchar_t wch);
Trying to just pass the QChar fails with an error:
error: C2664: 'myFunc' : cannot convert parameter 1 from 'QChar' to 'wchar_t'

Found the answer even while I was asking the question, I needed to use QChar::unicode().
wchar_t wch = qch.unicode();

Related

C++ error:invalid conversion from 'int' to 'const wchar_t* because of swprintf

the file I want to compile
first error:
cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}|
I fixed it by changing WriteConsoleOutputCharacter to WriteConsoleOutputCharacterW.
However I get another error on this line:
swprintf(screen, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f/fElapsedTime);
invalid conversion from 'int' to 'const wchar_t*' [-fpermissive]|
initializing argument 2 of 'int swprintf(wchar_t*, const wchar_t*, ...)'|
How can I compile this code?
The swprintf function you're using does not appear to take a buffer size. You may want to consider using _snwprintf instead.
_snwprintf(screen, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f/fElapsedTime);
You may get warned that this function is unsafe - the suggested alternative is _snwprintf_s where you must specify both the size of the buffer and the maximum number of characters to write. In practice, these are often the same, so most of the time, calls to this function look like this:
_snwprintf_s(screen, 40, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f/fElapsedTime);

'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'

I'm still rather new to C++ but I've ran into a problem I can't solve, this is my error message:
'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'
And this the line that is throwing the error:
ModuleHandle = (DWORD)GetModuleHandle(moduleName.c_str());
You are passing a char * to something that needs a wchar_t *. You'll have to either convert your stringtype to wchar_t *, for example using the MultiByteToWideChar function (https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072%28v=vs.85%29.aspx), or you can use the non-wide version of GetModuleHandle by calling GetModuleHandleA() instead of GetModuleHandleW().

C++ error C2664 with CStrings

I have some old C++ file which I know used to compile. I have created a new install of Visual C++ version 6.
I am getting lots of compile errors with CStrings about not being able to convert to const char *
Here's an example.
CString dogs = "test";
writeoutfile(dogs, 1);
void Crender::writeoutfile(CString data, long data_size) {}
I get this error:
error C2664: 'void __thiscall Crender::writeoutfile(const char *,long)' : cannot convert parameter 1 from 'class CString' to 'const char *'
Is there some way I can get round this?
You have to get the raw pointer to the char field. This can be done with
CString::GetBuffer()
so you could call
writeoutfile(dogs.GetBuffer(), 1);
CString should convert to const char*. Is it a Unicode build? That's the only explanation I can think of.
GetBuffer() is for getting a writeable pointer to the data contained inside CString. Don't do that!

Initializing auto variables in VC++ 2012

I am new to VC++ 2012. I have this code snippet.
auto it = query_map.find(U("callback"));
The problem is right under the dot there is a red line the error is
Error 1 error C2664: 'std::_Tree_iterator std::_Tree::find(const http::uri::encoded_string &)' : cannot convert parameter 1 from 'const wchar_t [9]' to 'const http::uri::encoded_string &' d:\maverick\projects\strikeforce\src\server\server\server.cpp 26
Can someone tell me a solution to this error?
Error 1 error C2664: 'std::_Tree_iterator std::_Tree::find(const http::uri::encoded_string &)'
This is a problem with the method find() from the class std::Tree
cannot convert parameter 1 from 'const wchar_t [9]' to 'const http::uri::encoded_string &'
The find() method must be used with a paremeter of type 'const http::uri::encoded_string &', but you passed a 'const wchar_t [9]'.
the method U() you have used returns an array of chars, but the find() method need another type of object.

How can I use SimpleIni to return wchar_t and then convert to std::wstring?

SimpleIni Documentation says wchar_t is supported but I don't understand how to use it. This is what I tried:
CSimpleIniCaseW ini;
ini.LoadFile("myapp.ini");
std::wstring test(ini.GetValue("testsection", "testkey", ""));
error C2664:
'CSimpleIniTempl::GetValue'
: cannot convert parameter 1 from
'const char [12]' to 'const wchar_t *'
You need to specify your strings as L"testsection" << Note the L.