Saturday, May 22, 2010

C2664 C++ error?

new to C++ language and I've been trying to learn a little about windows programming, here's the code I have so far





[code]


#include "windows.h"





int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,


LPSTR lpCmdLine, int nShowCmd)





{


MessageBox(NULL, "All your base are belong to us!",


"Hello World", MB_OK | MB_ICONEXCLAMATION);


}


[/code]





and the compile error I get:


------ Build started: Project: Window, Configuration: Debug Win32 ------


Compiling...


Window.cpp


c:\documents and settings\john tripi\my documents\visual studio 2005\projects\brand new\my calculator\window\window\window.cpp(8) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [32]' to 'LPCWSTR'


Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Build log was saved at "file://c:\Documents and Settings\John Tripi\My Documents\Visual Studio 2005\Projects\Brand New\My Calculator\Window\Window\Debug\BuildLog....


Window - 1

C2664 C++ error?
Ah, yes, I see the problem. The newest Visual Studio prefers unicode strings, which use 16 bits per character, as opposed to the classic strings which used 8. When unicode is defined, it uses MessageBoxW (W is for wide character), normally it would use MessageBoxA. The solution is to make your string literals into wide character strings by prefixing them with L:





MessageBox(NULL, L"All your base are belong to us!",


L"Hello World", MB_OK | MB_ICONEXCLAMATION);

wedding flowers

No comments:

Post a Comment