vsinha
asked on
'.c' vs '.cpp'
I have a program called 'app.c' that opens a dialog box. However when I rename the file to 'app.cpp' I get an error.
The line that gives the error is:
DialogBox ( hinstance, "SelWND", hwnd, WndDialogProc) ;
and the error message is:
error C2664: 'DialogBoxParamA' : cannot convert parameter 4 from 'int (void *,unsigned int,unsigned int,long)' to 'int (__stdcall *)(void)'
Declaration for WndDialogProc is :
BOOL CALLBACK WndDialogProc (HWND, UINT, WPARAM, LPARAM) ;
Why does this error happen? I wanted to use classes/C++ without using MFC.
The line that gives the error is:
DialogBox ( hinstance, "SelWND", hwnd, WndDialogProc) ;
and the error message is:
error C2664: 'DialogBoxParamA' : cannot convert parameter 4 from 'int (void *,unsigned int,unsigned int,long)' to 'int (__stdcall *)(void)'
Declaration for WndDialogProc is :
BOOL CALLBACK WndDialogProc (HWND, UINT, WPARAM, LPARAM) ;
Why does this error happen? I wanted to use classes/C++ without using MFC.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You need to #define STRICT, which means to do strict type checking
It ensures that handle types and function prototypes much match exactly. With C it helps catch errors, with C++ it makes code compile (as C++ already checks that function pointers match ok)
When STRICT is on, DLGPROC is defined as
typedef BOOL (CALLBACK* DLGPROC)(HWND, UINT, WPARAM, LPARAM);
When it is off it is just
typedef FARPROC DLGPROC;
where FARPROC is just typedef int (FAR WINAPI *FARPROC)();
This won't match with your WndDialogProc function in C++. This is because in C the () in a function pointer declaration means there could be any args at all, and so arg type checking isn't done. In C++ arg type checking is always done and () means no args at all - the same as (void).
In general is is a good idea to always use #define STRICT for both C and C++. But it is almsot ESSENTIAL for C++. (C may compile without it, but C++ won't).
I hope this clears things up for you.
It ensures that handle types and function prototypes much match exactly. With C it helps catch errors, with C++ it makes code compile (as C++ already checks that function pointers match ok)
When STRICT is on, DLGPROC is defined as
typedef BOOL (CALLBACK* DLGPROC)(HWND, UINT, WPARAM, LPARAM);
When it is off it is just
typedef FARPROC DLGPROC;
where FARPROC is just typedef int (FAR WINAPI *FARPROC)();
This won't match with your WndDialogProc function in C++. This is because in C the () in a function pointer declaration means there could be any args at all, and so arg type checking isn't done. In C++ arg type checking is always done and () means no args at all - the same as (void).
In general is is a good idea to always use #define STRICT for both C and C++. But it is almsot ESSENTIAL for C++. (C may compile without it, but C++ won't).
I hope this clears things up for you.
ASKER
Thank a lot!
btw, what is the advantage of not having #define STRICT ? or, in other words why does it exist, it seems to me that all programs should always use it.
btw, what is the advantage of not having #define STRICT ? or, in other words why does it exist, it seems to me that all programs should always use it.
For compatibility with code that was (poorly) ported from Win3.x.
Shame I didn't get offered any points for my lucid explanation .. that's life I guess :-(
ASKER
Ronslow, check Q.10048982
ASKER
#define STRICT
why is it needed? what does it mean?