Thread: Macros as functions

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    43

    Macros as functions

    Why isn't this working?

    Macros:

    Code:
    #define KEYDOWN ( vKey ) ( ( GetAsyncKeyState ( vKey ) & 0x8000 ) ? TRUE  : FALSE )
    #define KEYUP   ( vKey ) ( ( GetAsyncKeyState ( vKey ) & 0x8000 ) ? FALSE : TRUE  )
    Global variables:
    Code:
    int vKey;
    Code:

    Code:
    if ( KEYDOWN ( VK_ESCAPE ) )
    	SendMessage ( hWnd, WM_CLOSE, 0, 0 );
    Error:

    Code:
    Error	1	error C2064: term does not evaluate to a function taking 1 arguments 161
    It works fine without the use of macros. What am I doing wrong?

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Could be
    - you didn't include the header file containing those macros
    - the macro is redefined later
    - you have a variable / function with the same name

    Code:
                                  -PREPROCESSOR-
    
    /C don't strip comments                  /FI<file> name forced include file
    /D<name>{=|#}<text> define macro         /U<name> remove predefined macro
    /E preprocess to stdout                  /u remove all predefined macros
    /EP preprocess to stdout, no #line       /I<dir> add to include search path
    /P preprocess to file                    /X ignore "standard places"
    If you use this option, you get a file after all the preprocessing has happened, which can help track down when macros start behaving oddly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Quote Originally Posted by Salem
    Could be
    - you didn't include the header file containing those macros
    - the macro is redefined later
    - you have a variable / function with the same name
    All of my code is in the same file. I searched my file, and there's no redefinitions or other variables and functions that have the same name.

    I tried the preprocessor option, but the generated file is 90000 lines long... Also, it gives me another error instead:

    Code:
    Error	1	fatal error LNK1104: cannot open file '.\Debug\Bouncing Ball.obj'	Bouncing Ball

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Spaces are very important to macro definitions. Try this:
    Code:
    #define KEYDOWN(vKey) ( ( GetAsyncKeyState ( vKey ) & 0x8000 ) ? TRUE  : FALSE )
    #define KEYUP(vKey)   ( ( GetAsyncKeyState ( vKey ) & 0x8000 ) ? FALSE : TRUE  )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Ah, thanks. I wasn't aware of that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. template fn replacements for msg macros
    By Ken Fitlike in forum Windows Programming
    Replies: 17
    Last Post: 10-30-2002, 07:55 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM