2 智能感知:类型 "const char *" 的参数与类型 "LPCWSTR" 的参数不兼容
2 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
堆栈流成员 ----我是 C++ WINAPI 的初学者 ---当我在我的第一个 WIN32 GUI 中编码时,出现了一些错误 [输出] --- 在C++ ---我的代码中是否有任何推荐的书籍:
#include <windows.h>
const char g_szClassName[] = ("myWindowClass");
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = (g_szClassName);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL,
L"Window Registration Failed!",
L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
(g_szClassName),
L"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
if(hwnd == NULL) {
MessageBox(NULL,
L"Window Creation Failed!",
L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
输出:
3 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR" c:UsersYoussefDocumentsVisual Studio 2012ProjectsWin32Project2Win32Project2Win32Project2.cpp 46 2 Win32Project2
2 IntelliSense: a value of type "const char *" cannot be assigned to an entity of type "LPCWSTR" c:UsersYoussefDocumentsVisual Studio 2012ProjectsWin32Project2Win32Project2Win32Project2.cpp 34 19 Win32Project2
Error 1 error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? c:usersyoussefdocumentsvisual studio 2012projectswin32project2win32project2win32project2.cpp 65 1 Win32Project2
您遇到的编译问题与字符串类型和 Windows 程序有关。
请注意,Windows 程序有两种字符集"构建类型"。 第一个是MBCS
,第二个是Unicode
. 默认情况下(假设您使用的是 Visual Studio),构建类型为 Unicode
,这意味着各种 Windows API 函数将处理宽字符串,而不是基于char
或char
的字符串。
解决问题的最简单方法是对字符数组使用 TCHAR
,对要从 Windows API 函数发送和接收的字符串文本使用_T()
宏。 这将确保您的应用程序将针对 Unicode 以及 MBCS 进行编译(尽管 MBCS 程序现在被认为是过时的。
这一行:
wc.lpszClassName = (g_szClassName);
给出了一个问题,因为您正在编译一个 Unicode 应用程序,而 WNDCLASSEX
的这个成员采用LPCTSTR
(指向 TCHAR
的指针),而不是const char *
。
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633577%28v=vs.85%29.aspx
所以对于这个问题,
const char g_szClassName[] = ("myWindowClass");
更改应为:
#include <tchar.h> // in case it isn't included
//...
TCHAR g_szClassName[] = _T("myWindowClass");
相关文章:
- 扩展C++生成的代码的模板参数类型名称
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 在 C++ 中运行时调用模板时,是否可以切换模板的参数类型?
- 将函数参数类型声明为 auto
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- 在 C++17 中调用具有不同参数类型的构造函数
- 具有先前参数类型匹配的参数包
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- 如何从第一个参数推断第二个参数类型?
- C++模板函数中,指定回调函子/lambda 的参数类型,同时仍允许内联?
- 如何用不同的参数类型和数字回调函数
- C++stoi:这两个重载都无法转换所有参数类型
- 为什么std::{container}::template不能推导其参数类型
- 为模板参数类型中的新对象分配内存
- 为指向成员模板参数的指针推导额外模板参数类型的紧凑方式
- 使用std::conditional中的模板来确定函数参数类型
- C++中的短参数类型