错误:不能转换 '常量为wchar_t [13]',以分配 'LPCSTR

2019-07-17 11:10发布

// include the basic windows header file
#include <windows.h>
#include <windowsx.h>

// the WindowProc function prototype LRESULT CALLBACK WindowProc(HWND hWnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam);

// the entry point for any Windows program int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow) {
    // the handle for the window, filled by a function
    HWND hWnd;
    // this struct holds information for the window class
    WNDCLASSEX wc;

    // clear out the window class for use
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    // fill in the struct with the needed information
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = L"WindowClass1";

    // register the window class
    RegisterClassEx(&wc);

    // create the window and use the result as the handle
    hWnd = CreateWindowEx(NULL,
                          L"WindowClass1",    // name of the window class
                          L"Our First Windowed Program",   // title of the window
                          WS_OVERLAPPEDWINDOW,    // window style
                          300,    // x-position of the window
                          300,    // y-position of the window
                          500,    // width of the window
                          400,    // height of the window
                          NULL,    // we have no parent window, NULL
                          NULL,    // we aren't using menus, NULL
                          hInstance,    // application handle
                          NULL);    // used with multiple windows, NULL

    // display the window on the screen
    ShowWindow(hWnd, nCmdShow);

    // enter the main loop:

    // this struct holds Windows event messages
    MSG msg;

    // wait for the next message in the queue, store the result in 'msg'
    while(GetMessage(&msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessage(&msg);
    }

    // return this part of the WM_QUIT message to Windows
    return msg.wParam; }

// this is the main message handler for the program LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    // sort through and find what code to run for the message given
    switch(message)
    {
        // this message is read when the window is closed
        case WM_DESTROY:
            {
                // close the application entirely
                PostQuitMessage(0);
                return 0;
            } break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProc (hWnd, message, wParam, lParam); }

===============我使用的代码块,这个代码是从直接X教程。

我收到以下错误:

||In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)':|
error: cannot convert 'const wchar_t [13]' to 'LPCSTR {aka const char*}' in assignment|
|49|warning: converting to non-pointer type 'DWORD {aka long unsigned int}' from NULL [-Wconversion-null]|
|49|error: cannot convert 'const wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'|
||=== Build finished: 2 errors, 1 warnings ===|

Answer 1:

您的项目不具有UNICODE定义的预处理器符号,以便采取指向字符串的Windows API函数期望char * ,而不是wchar_t * 。 更改

L"WindowClass1"

"WindowClass1"

执行相同的剩余字符串文字。 或者,将其更改为_T("WindowClass1")这将扩大到字面基础上,正确类型的字符串的UNICODE被定义的符号。


我的建议是去你的项目属性,并更改Character Set设置Unicode ,然后使用所有Windows API函数的宽字符版本明确。 例如,而不是CreateWindow ,叫CreateWindowW

编辑:
该项目的设置,我只建议适用于Visual Studio中,不知道怎么做,在代码::块。



Answer 2:

1)如果你想使用Unicode编译,然后更改选项。 如果你是从IDE编译时,设置以下属性格式配置属性 - >常规 - >项目默认设置 - >字符集 - >使用Unicode字符集。

如果在命令行中使用选项/ DUNICODE / D_UNICODE编译

如果你不想使用Unicode编译,只要按照步骤2和3中。 在字符集,不要选择UNICODE。

2)前

#include <windows.h>

#include <tchar.h>

3)改变

wc.lpszClassName = L"WindowClass1";

wc.lpszClassName = _T("WindowClass1");

如果你想使用Unicode编译,你可以得到的只是做#1,但最好做的所有3。

如果你想不UNICODE编译,执行#2&#3 - 不做#1。



Answer 3:

我用这对我的单一源代码的文件,因为我不想改变我目前的项目配置

#ifndef UNICODE
#define UNICODE
#define UNICODE_WAS_UNDEFINED
#endif

#include <Windows.h>

#ifdef UNICODE_WAS_UNDEFINED
#undef UNICODE
#endif


文章来源: error: cannot convert 'const wchar_t [13]' to 'LPCSTR {aka const char*}' in assignment