Multiple errors where none occurred before

This is a discussion on Multiple errors where none occurred before within the Windows Programming forums, part of the Platform Specific Boards category; greetings I have just installed Visual Studio 2012 and after MANY years of doing nothing I tried out some of ...

Multiple errors where none occurred before

greetings I have just installed Visual Studio 2012 and after MANY years of doing nothing I tried out some of my old code which used to work fine but now generates multiple errors. Even MessageBox says I need four parameters but I can get around this by using _T( ) around the text?

TCHAR instead of char stops errors that did not occur before in declaring variable names?

Note to mods: This is a windows programming question, and belongs in that forum, not in the C++ forum.

You're barking up the wrong tree with using _T() macro or TCHAR type. Those are related to default character widths (eg allowing code to work whether built using unicode or not), not to fixing problems with number of arguments a function is called with.

MessageBox() has always accepted four arguments - at least, since the first version of the windows API that shipped with Windows 1.

However, historically, C compilers allowed usage of undeclared functions: when code calls a function that had not been declared, the compiler would assume the function accepts a variable argument list. The C++ standard made that flatly illegal. Using that feature has been consider bad practice in C for a few decades, so such things have been deprecated in recent C standards.

So, in short, the code you used "successfully" before was actually broken, and more recent compilers now detect that it is broken.

The argument your code is missing is actually the first: a pointer to a window that will own/manage the MessageBox. That pointer can be NULL (indicating no owner window), so the easiest solution would be to

Code:

MessageBox(0, "You must enter at least one character!", "Error!", MB_OK);

or (more completely)

Code:

MessageBox((HWND)NULL, "You must enter at least one character!", "Error!", MB_OK);

It is mandatory if compiling as C++ (and really good practice if compiling as C) to ensure the compiler can see a declaration of MessageBox() before the code that calls it. That can be achieved by #include <winuser.h> (which is Microsoft specific, but shouldn't cause problems, assuming you are building a windows application).

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

MessageBox is a function in the windows API (currently win32 API) and, given that you didn't specify things further and references to need 4 arguments, I assumed you were not using MFC.

You're using a MessageBox that is a member function of CWnd, which is a MFC class. MFC is a class library, which is built on top of the win32 API.

I suspect you're taking code which was developed using MFC, and putting parts of it into a context that doesn't involve using MFC.

Either way, when building applications for windows, one of the build options is using wide characters (unicode) or not. When you build for unicode, MessageBox is a macro that expands to MessageBoxW() [I can't remember, offhand, what MessageBox expands to if not building for unicode, but it is a different function]. MessageBoxW() does require the two string arguments to be wide character strings, hence need to use _T() macro (the _T() macro does things differently as well, depending on whether you're building for unicode or not). That is true for both win32 and MFC (the only difference being that win32's MessageBox requires 4 arguments to MFC's CWnd::MessageBox() requiring 3).

Presumably your code was build without unicode (which means it either predated unicode, or the developer simply set build options to not use unicode and didn't bother further). So you need to look at your project options, and turn off unicode (or wide character) support, or use the _T() macro.

Either way, you need to make sure project build options are consistent with what your code is assuming. The simplest is probably to turn off unicode support, in the first instance. (Historically, unicode was introduced later, and became a default). That is a Microsoft change, not a C/C++ change.

So, in short, you need to look at your code and building it in a holistic manner.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.