-3

Its been difficult trying to find an explanation online on the below syntax and searching for L"" in these forums doesn't provide any results. I'm inside Qt5 debugging an application which is too lengthy to paste here. I'm trying to resolve this last error that points to the line of code containing --> return L""; <--. Its one of the last lines in the function below. The compiler error i'm receiving is...

C2440: 'return': cannot convert from 'const wchar_t[1]' to 'LPWSTR'

My first question is what does this mean --> L"" <-- ?

Lastly, how can I resolve this compiler error?

LPWSTR ImageThread::getLastErrorString(DWORD lastError)
    {
        DWORD result = FormatMessage(
            FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            lastError,
            LANG_USER_DEFAULT,
            errorString,
            sizeof(errorString),
            NULL);
        if (result > 0)
        {
            return errorString;
        }
        else
        {
            return L"";
        }
    }

My assumption is because wchar_t[1] is 1 byte and LPWSTR is 2 bytes it's not able to convert??? I'm not for sure if this is important but I'm getting another Error up in the tool bar of Qt5 saying "Error: Could not decode "myfile.cpp" with "UTF-8" encoding. Editing not possible". It gives me an option to select Encoding. I'm sure this is due to the fact that this code was written with an older version of Qt and now because I'm rebuilding all this code with Qt5 there has been some changes. Again, I'm not for sure this relates to my two questions above but I wanted to paint a complete picture of what's going on.

bAsH
  • 25
  • 4
  • _searching for L"" in these forums doesn't provide any results_ SO is **not** a forum! Update: _My assumption is because wchar_t[1] is 1 byte and LPWSTR is 2 bytes it's not able to convert_ How did you make the conclusion that `wchar_t[1]` is one byte? Or `LPWSTR` is 2 bytes? In general, the compiler is complaining about the `L""` being `const`, while your function returns non-`const` value. – Algirdas Preidžius Jan 17 '17 at 16:48
  • http://stackoverflow.com/questions/6384118/what-does-the-l-in-front-a-string-mean-in-c – Alexander V Jan 17 '17 at 16:48
  • 1
    I can't say anything about the error, but you can read about string literal prefixes like `L""` here: http://en.cppreference.com/w/cpp/language/string_literal – HolyBlackCat Jan 17 '17 at 16:48
  • The error isn;t to do with the L prefix but because const has been assigned away without an explicit cast. However MS have got themselves in a twist with all the wchar, WCHAR wchar_t stuff. Try to use UTF8 and just cast to MS at the last moment, when actually interfacing with a Windows function. – Malcolm McLean Jan 17 '17 at 16:52
  • In order to make things like `L""` to compile you need to define `UNICODE` preprocessor symbol and probably include `windows.h` in your code. – vahancho Jan 17 '17 at 16:52
  • @vahancho No, you don't need any of those. Read the duplicate. – Algirdas Preidžius Jan 17 '17 at 16:54
  • `LPWSTR` is a typedef of `wchar_t*`, while `L""` is of type `const wchar_t[1]`. If possible, you should change the return type of your function to LPCWSTR, which is a typedef of `const wchar_t*`. Otherwise cast the return value to `LPWSTR`. – Joseph Artsimovich Jan 17 '17 at 16:57
  • @AlgirdasPreidžius I'm alittle lost on how to resolve this issue. Would you be so kind to elaborate on a fix? – bAsH Jan 17 '17 at 21:39
  • nevermind... figured it out. thanks anyways – bAsH Jan 17 '17 at 21:48

1 Answers1

2

An "L" (or 'l', though most recommend against it, because of how much it looks like a 1) before a string literal (or character literal) makes it a "wide" literal, so what it contains are wchar_t instead of char, and its address is a wchar_t * instead of a char *.

The C and C++ standards don't specify precisely what wide characters are. On Windows, they'll normally be 16 bits representing the string in UTF-16. On most Unix-like systems, they'll normally be 32 bits, representing the string in UCS-4.

In this particular case, the "" after the L means it's just returning an empty string.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111