C++菜鸟提问??

来源:百度知道 编辑:UC知道 时间:2024/05/13 02:20:23
新建了个空的Win32 Project 新建了个.cpp文件,敲下如下代码,提示出错?
#include<windows.h>
int WINAPI WinMian(HINSTANCE hInstacnce,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
MessageBox(NULL,"HelloWorld!","say hello",0);
return 0;
}
Error 1 error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR' e:\symbiancase\helloworld\helloworld\exghelloworld.cpp 4这是什么错了呢??

你这里好像定义了UNICODE宏。
MessageBox是MessageBoxW了。所以要这样:

MessageBox(NULL,L"HelloWorld!",L"say hello",0);

更好的解决办法是用_T宏
MessageBox(NULL,_T("HelloWorld!"),_T("say hello"),0);

如果没有包含相应文件,_T宏是这样定义的,可以手动加上。

#ifdef UNICODE
#define _T(x) L##x
#else
#define _T(x) x
#endif

另外 int WINAPI WinMian好像也不对。可能要改成 int wWinMain 这个我不太清楚。

这里不能用常量。这样做:
CString str;
str="Helloworld";
MessageBox(NULL,str,"say hello",0);

宽字符, L"say hello"

::MessageBox 最好这样, 否则可能会有namespace问题