c ++在win32学校应用程序中构建错误 [英] c++ build errors on win32 school application
问题描述
我没有在c ++编程非常长,但需要为我的学校做一个Win32应用程序。
错误:
pre>错误C2440:'=':无法从'const char [11]'转换为'LPCWSTR'
错误C2664:'CreateWindowExW':无法将参数2从'const
错误C2664:'TextOutW':不能将参数4从'char *'转换为'LPCWSTR'
IntelliSense:类型char *的参数与参数不兼容类型LPCWSTR
不知道如果所有其他的是正确的,那些4错误现在
cpp文件:
/ * Hoofdstuk 10 ,用户界面* /
#includeCallback_NYCM.h
// UI
int WINAPI WinMain(HINSTANCE thisInstance,HINSTANCE prevInstance,LPSTR lpCmdLine,int nShowCmd)
{
PAINTSTRUCT ps;
HDC hdc;
MSG msg;
HWND hwnd;
WNDCLASSEX wndclassex; // struct_WNDCLASSEX via windows.h
// toekenning
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = thisInstance;
wndclassex.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclassex.hCursor = LoadCursor(thisInstance,IDC_ARROW);
wndclassex.hbrBackground =(HBRUSH)(COLOR_WINDOW + 1);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName =WNDCLASSEX;
wndclassex.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
// functie aanroep
RegisterClassEx(& wndclassex);
// IfThen - > CreateWindows
if(!(hwnd = CreateWindowEx(NULL,WNDCLASSEX,Hoofdstuk 10,WS_OVERLAPPEDWINDOW
| WS_VISIBLE,50,50,650,300,NULL,NULL,thisInstance,NULL)))
{
return 0;
}
// logische structuur
while(GetMessage(& msg,NULL,0,0))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(& msg);
DispatchMessage(& msg);
}
return(int)msg.wParam;
};
头文件:
/ * Hoofdstuk 10,Deelnemer.h * /
// Declaratie
class Deelnemer
{
private:
char * nm;
public:
//构造函数
Deelnemer(){
}
// Methoden = prototype
void Deelnemer :: Invoeren ;
char * Deelnemer :: WeergevenNaam();
};
//Implemenmtatie.Invoeren
void Deelnemer :: Invoeren()
{
nm =Roy;
}
//.Weergeven
char * Deelnemer :: WeergevenNaam()
{
return nm;
}
callback_NYCM.h:
/ * Hoofdstuk 10,Callback_NYCM * /
#includeWindows.h
#includeDeelnemer.h
//原型
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam);
//实现
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
//构造
PAINTSTRUCT ps;
HDC hdc;
MSG msg;
WNDCLASSEX wndclassex;
// HWND hwnd;
Deelnemer deelnemer1;
// UI
切换(消息)
{
case WM_PAINT:
{
//Functieaanroep.Initialisatie
deelnemer1.Invoeren();
//.TextOut
TextOut(hdc,50,50,deelnemer1.WeergevenNaam(),
strlen(deelnemer1.WeergevenNaam()));
EndPaint(hwnd,& ps);
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
默认值:
{
return DefWindowProc(hwnd,message,wparam,lparam);
}
break;
}
return 0;
}
我认为我的构造函数或类似的东西是错误的, * Deelnemer :: WeergevenNaam()
有人可以解释我的代码有什么问题,所以我知道如何让它工作吗?
UPDATE:
更新应用程序需要使用UNICODE字符串
,即LMyString而不是MyString。你还需要
使用WCHAR / wchar_t代替char
但是如何做我的代码,有人帮忙吗?
更新2:
解决了很多错误!
$ b $ Deelnemer deelnemer1;
switch(message)
{
case WM_PAINT:
{
//Functieaanroep.Initialisatie
deelnemer1.Invoeren();
//.TextOut
TextOut(hdc,50,50,deelnemer1.WeergevenNaam(),
strlen(deelnemer1.WeergevenNaam()));
EndPaint(hwnd,& ps);
return 0;
}
所以错误在线:deelnemer1.WeergevenNaam()
-TextOutW':无法将参数4从'char *'转换为'LPCWSTR'
-IntelliSense: char *与类型LPCWSTR的参数不兼容
更新3:
一个解决方案(像你们下面说的)但现在我只有这一个离开:
TextOut(hdc,50,50,deelnemer1.WeergevenNaam(),
/ on the deelnemer1.weergevenNaam()
错误C2664:'TextOutW':无法将参数4从'const char *'转换为'LPCWSTR'
您的代码编写为ANSI编译,但您的解决方案设置包括 _UNICODE
/ UNICODE
。您可以通过更改字符集(在配置属性的常规节点上)从使用Unicode字符集到使用多字节字符集或更新您的应用程序代码(建议后者)。
更新您的应用程序需要使用UNICODE字符串字面,即 LMyString
而不是MyString
。您还需要使用 WCHAR
/ wchar_t
替换 char
(如果适用)并调用宽版本的Windows API。对于许多API调用,存在一个宽泛的版本,最后有一个 W
,例如。 CreateWindowExW
。如果您使用的是标准C ++库,您还需要确保使用需要字符编码的UNICODE变体(例如 std :: wstring
而不是 std :: string
)。有关其他信息,请参见 Visual C ++中的文本和字符串。 p>
有关这里发生了什么的更多背景:Windows API和Microsoft的CRT实现可以用于使用ANSI /多字节字符集或UNICODE字符集编译代码。为了支持两种字符编码,C / C ++预处理器根据 _UNICODE
和 UNICODE是否替换相应的字符类型和API调用 $
例如,调用 CreateWindowEx
CreateWindowExA
或 CreateWindowExW
。两个实现对于字符串参数(分别为 char *
和 wchar_t *
)具有不同的参数类型。要使用ANSI /多字节编码,调用将是 CreateWindowExA(NULL,WNDCLASSEX,...)
。对于UNICODE它看起来像 CreateWindowExW(NULL,LWNDCLASSEX,...)
。
在预处理程序通过之后代码的外观,您可以使用 / P 或 / E 编译器开关(假设您使用的是Microsoft编译器)。
注意:不要忘记阅读绝对最小的每个软件开发人员绝对必须了解Unicode和字符集(无需借口!)。
I haven't been programming in c++ very long, but need to make an Win32 application for my school. The teacher helped me a lot with information but after a few days of trying I am still stuck.
Errors:
error C2440: '=' : cannot convert from 'const char [11]' to 'LPCWSTR'
error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [11]' to 'LPCWSTR'
error C2664: 'TextOutW' : cannot convert parameter 4 from 'char *' to 'LPCWSTR'
IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR"
Don't know if all the other suff is right, but i only get those 4 error now
cpp file:
/* Hoofdstuk 10, User Interface */
#include "Callback_NYCM.h"
// UI
int WINAPI WinMain(HINSTANCE thisInstance,HINSTANCE prevInstance,LPSTR lpCmdLine,int nShowCmd)
{
PAINTSTRUCT ps;
HDC hdc;
MSG msg;
HWND hwnd;
WNDCLASSEX wndclassex; //struct_WNDCLASSEX via windows.h
// toekenning
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WndProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = thisInstance;
wndclassex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclassex.hCursor = LoadCursor(thisInstance,IDC_ARROW);
wndclassex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = "WNDCLASSEX";
wndclassex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// functie aanroep
RegisterClassEx(&wndclassex);
// IfThen -> CreateWindows
if(!(hwnd = CreateWindowEx(NULL,"WNDCLASSEX","Hoofdstuk 10",WS_OVERLAPPEDWINDOW
| WS_VISIBLE,50,50,650,300,NULL,NULL,thisInstance,NULL)))
{
return 0;
}
// logische structuur
while(GetMessage(&msg, NULL, 0, 0))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
};
header file:
/*Hoofdstuk 10, Deelnemer.h*/
//Declaratie
class Deelnemer
{
private:
char* nm;
public:
//Constructor
Deelnemer(){
}
//Methoden = prototype
void Deelnemer::Invoeren();
char* Deelnemer::WeergevenNaam();
};
//Implemenmtatie.Invoeren
void Deelnemer::Invoeren()
{
nm = "Roy";
}
//.Weergeven
char* Deelnemer::WeergevenNaam()
{
return nm;
}
callback_NYCM.h:
/*Hoofdstuk 10, Callback_NYCM*/
#include "Windows.h"
#include "Deelnemer.h"
// prototype
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam);
//Implementatie
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
//Constructie
PAINTSTRUCT ps;
HDC hdc;
MSG msg;
WNDCLASSEX wndclassex;
// HWND hwnd;
Deelnemer deelnemer1;
//UI
switch(message)
{
case WM_PAINT:
{
//Functieaanroep.Initialisatie
deelnemer1.Invoeren();
//.TextOut
TextOut(hdc,50,50,deelnemer1.WeergevenNaam(),
strlen(deelnemer1.WeergevenNaam()));
EndPaint(hwnd,&ps);
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
default:
{
return DefWindowProc(hwnd,message,wparam,lparam);
}
break;
}
return 0;
}
I think my constructor or something like that is wrong and my return value for char* Deelnemer::WeergevenNaam()
Could somebody explain me what is wrong in my code so I know how to get it working?
UPDATE:
Updating your application requires to use UNICODE string literals throughout, i.e. L"MyString" instead of "MyString". You also need to use WCHAR/wchar_t in place of char
But how do i do this with my code, could somebody help?
UPDATE 2:
That solved al lot of errors!
But i have some more errors left in this part
Deelnemer deelnemer1;
switch(message)
{
case WM_PAINT:
{
//Functieaanroep.Initialisatie
deelnemer1.Invoeren();
//.TextOut
TextOut(hdc,50,50,deelnemer1.WeergevenNaam(),
strlen(deelnemer1.WeergevenNaam()));
EndPaint(hwnd,&ps);
return 0;
}
so the errors are on line: deelnemer1.WeergevenNaam()
-TextOutW' : cannot convert parameter 4 from 'char *' to 'LPCWSTR'
-IntelliSense: argument of type "char *" is incompatible with parameter of type "LPCWSTR"
UPDATE 3:
After some testing i found a solution (like you guys said below) But now i only got this one left:
TextOut (hdc,50,50,deelnemer1.WeergevenNaam(),
// on the deelnemer1.weergevenNaam()
with error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char *' to 'LPCWSTR'
Your code is written to compile as ANSI but your solution settings include _UNICODE
/UNICODE
. You can either set your solution to use ANSI encoding by changing Character Set (on the General node of the Configuration Properties) from Use Unicode Character Set to Use Multi-Byte Character Set or update your application code (the latter is recommended).
Updating your application requires to use UNICODE string literals throughout, i.e. L"MyString"
instead of "MyString"
. You also need to use WCHAR
/wchar_t
in place of char
(where applicable) and call the wide versions of the Windows API. For many API calls there exists a wide version that has a W
at the end, e.g. CreateWindowExW
. If you are using the Standard C++ Library you will also want to make sure to use the UNICODE variants where character encoding is necessary (e.g. std::wstring
instead of std::string
). Additional information can be found at Text and Strings in Visual C++.
A bit more background about what is going on here: The Windows API and Microsoft's CRT implementation can be used to compile code using ANSI/Multi-Byte character set or UNICODE character set. To support both character encodings the C/C++ preprocessor replaces the respective character types and API calls with the specific implementations depending on whether or not the _UNICODE
and UNICODE
preprocessor symbol is defined.
For example, the call CreateWindowEx
is expanded to either CreateWindowExA
or CreateWindowExW
. Both implementations have different parameter types for string arguments (char*
and wchar_t*
respectively). To use ANSI/Multi-Byte encoding the call would be CreateWindowExA(NULL,"WNDCLASSEX",...)
. For UNICODE it would look like CreateWindowExW(NULL,L"WNDCLASSEX",...)
.
To see what the code looks like after the preprocessor is through you can use the /P or /E compiler switches (assuming you're using the Microsoft Compiler).
Note: Don't forget to read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).
这篇关于c ++在win32学校应用程序中构建错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!