#define - Expected and Expression C ++

I'm writing an application hat will use the same form defaults on most of the forms, so i decided to macro the WNDClASSEX and call it when needed:

#pragma once

#ifndef WNDCLASSEX_H
#define WNDCLASSEX_H

#include <windows.h>

//MAIN FORM / LOADER WNDCLASSEX
#define MainLoaderWnd(Size, WindowsProcess, hInstance, Title)\
{\
    return new WNDCLASSEX{\
    size,\
    CS_DBLCLKS,\
    WindowsProcess,\
    0,\
    0,\
    hInstance,\
    LoadIcon(NULL, IDI_APPLICATION),\
    LoadCursor(NULL, IDC_ARROW),\
    (HBRUSH)(COLOR_WINDOW),\
    NULL,\
    L(Title),\
    LoadIcon(NULL, IDI_APPLICATION)\
};\
}

#endif

however, when i add this into the file "loader.cpp", i get the following:

WNDCLASSEX wcex = MainLoaderWnd(sizeof(WNDCLASSEX), WndProc, hInstance, L"Your Text Here");
//Intellisense error: Expression Expected

Ideas/ can't see why his error occurs. :/

Upvotes: 0

Views: 1627

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409196

A preprocessor macro is not a function. What the preprocessor does when it sees MainLoaderWnd being "called" in your code is to replace the "call" with the text in the macro body. This means you assignment will look like this:

WNDCLASSEX wcex = { return new WNDCLASSEX{ ... }; }

This is not a valid assignment.

Instead you could create an inline function, which is like a proper function but the compiler (not the preprocessor) may put the generated code inline at the place of the call:

//MAIN FORM / LOADER WNDCLASSEX
inline PWNDCLASSEX MainLoaderWnd(size_t Size, WNDPROC WindowsProcess,
                                 HINSTANCE hInstance, LPTSTR Title)
{
    PWNDCLASSEX cls = new WNDCLASSEX;

    // Set the fields

    return cls;
}

Upvotes: 4

Related Questions