Default arguments, one call compiles, the other does not

E

Eric Lilja

This complete program compiles just fine:
void foo(int, int = 0);

void foo(int a, int b)
{
(void)a;
(void)b;
}

int
main()
{
foo4711);
}

However, this part of the *real* program does not:
void register_window_class(const char *class_name,
WNDPROC proc,
HBRUSH background,
const char * = NULL,
HICON = LoadIcon(0, IDI_APPLICATION),
HCURSOR = LoadCursor(0, IDC_ARROW),
UINT = CS_HREDRAW | CS_VREDRAW,
int = 0,
int = 0);

void register_class(const char *class_name,
WNDPROC proc,
HBRUSH background,
const char *menu_name,
HICON icon,
HCURSOR cursor,
UINT style,
int cbWndExtra,
int cbClsExtra)
{
WNDCLASS wndclass;

ZeroMemory(&wndclass, sizeof(WNDCLASS));

wndclass.style = style; /* Usually CS_HREDRAW | CS_VREDRAW */
wndclass.lpfnWndProc = proc;
wndclass.cbClsExtra = cbClsExtra; /* Usually 0 */
wndclass.cbWndExtra = cbWndExtra; /* Usually 0 */
wndclass.hInstance = NULL;
wndclass.hIcon = icon; /* Usually LoadIcon(0, IDI_APPLICATION) */
wndclass.hCursor = cursor; /* Usually LoadCursor(0, IDC_ARROW) */
wndclass.hbrBackground = background; /*
(HBRUSH)GetStockObject(WHITE_BRUSH) */
wndclass.lpszMenuName = menu_name; /* NULL or
MAKEINTRESOURCE(MENU_ID) */
wndclass.lpszClassName = class_name;

ATOM a = RegisterClass(&wndclass);

assert(a != 0);
}

HWND
MainWindow::create()
{
const char *application_name = "Test MyMFC"; /* Doubling as class
name. */

register_class(application_name, MainWindow::entry,
(HBRUSH)GetStockObject(WHITE_BRUSH));

g++ -Wall -W -ansi -pedantic -g -O0 -c MainWindow.cpp
MainWindow.cpp: In member function `HWND__* MainWindow::create()':
MainWindow.cpp:22: error: too few arguments to function `void
register_class(const char*, LRESULT (*)(HWND__*, UINT, WPARAM, LPARAM),
HBRUSH__*, const char*, HICON__*, HICON__*, UINT, int, int)'
MainWindow.cpp:48: error: at this point in file

Line 48 is the call to register_class...how do I make it compile while
keeping my default arguments? Sorry for posting an incomplete snippet
filled to the brim with platform specific code, but what was supposed
to be my exhibition program failed to exhibit the problem I need help
solving. =/

/ E
 
R

red floyd

Eric said:
This complete program compiles just fine:
void foo(int, int = 0);

void foo(int a, int b)
{
(void)a;
(void)b;
}

int
main()
{
foo4711);
}

However, this part of the *real* program does not:
void register_window_class(const char *class_name,
WNDPROC proc,
HBRUSH background,
const char * = NULL,
HICON = LoadIcon(0, IDI_APPLICATION),
HCURSOR = LoadCursor(0, IDC_ARROW),
UINT = CS_HREDRAW | CS_VREDRAW,
int = 0,
int = 0);

void register_class(const char *class_name,
WNDPROC proc,
HBRUSH background,
const char *menu_name,
HICON icon,
HCURSOR cursor,
UINT style,
int cbWndExtra,
int cbClsExtra)
{
WNDCLASS wndclass;

ZeroMemory(&wndclass, sizeof(WNDCLASS));

wndclass.style = style; /* Usually CS_HREDRAW | CS_VREDRAW */
wndclass.lpfnWndProc = proc;
wndclass.cbClsExtra = cbClsExtra; /* Usually 0 */
wndclass.cbWndExtra = cbWndExtra; /* Usually 0 */
wndclass.hInstance = NULL;
wndclass.hIcon = icon; /* Usually LoadIcon(0, IDI_APPLICATION) */
wndclass.hCursor = cursor; /* Usually LoadCursor(0, IDC_ARROW) */
wndclass.hbrBackground = background; /*
(HBRUSH)GetStockObject(WHITE_BRUSH) */
wndclass.lpszMenuName = menu_name; /* NULL or
MAKEINTRESOURCE(MENU_ID) */
wndclass.lpszClassName = class_name;

ATOM a = RegisterClass(&wndclass);

assert(a != 0);
}

HWND
MainWindow::create()
{
const char *application_name = "Test MyMFC"; /* Doubling as class
name. */

register_class(application_name, MainWindow::entry,
(HBRUSH)GetStockObject(WHITE_BRUSH));

g++ -Wall -W -ansi -pedantic -g -O0 -c MainWindow.cpp
MainWindow.cpp: In member function `HWND__* MainWindow::create()':
MainWindow.cpp:22: error: too few arguments to function `void
register_class(const char*, LRESULT (*)(HWND__*, UINT, WPARAM, LPARAM),
HBRUSH__*, const char*, HICON__*, HICON__*, UINT, int, int)'
MainWindow.cpp:48: error: at this point in file

Line 48 is the call to register_class...how do I make it compile while
keeping my default arguments? Sorry for posting an incomplete snippet
filled to the brim with platform specific code, but what was supposed
to be my exhibition program failed to exhibit the problem I need help
solving. =/
Take a look at the forward declaration of register_window_class, and
then the actual declaration of register_class. Hint:
register_window_class is not register_class. register_class has no
default params.
 
E

Eric Lilja

red said:
Take a look at the forward declaration of register_window_class, and
then the actual declaration of register_class. Hint:
register_window_class is not register_class. register_class has no
default params.

LOL! I must be blind not seeing that! Thanks for the quick response,
now everything works just fine.

/ E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top