CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2008
    Posts
    15

    Function Macro definition problem

    Hi all,

    I have a macro function defined as follows in xx.h:
    Code:
    int swap_tmp_int;
    #define INT_SWAP(X,Y)  swap_tmp_int=X; X=Y; Y=swap_tmp_int;
    Then I used this function in another cpp file and got the following error when built:

    "swap_tmp_int first defined here" Please kindly suggest.

    Thank you,
    Eric

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function Macro definition problem

    You can't define a global variable like swap_tmp_int in a header file.

    However, in this case there's no need. Just #include <algorithm> and use std::swap().

    Or, if you prefer, write it as an inline function rather than a macro so that it can have a local variable. You should prefer inline functions over macros most of the time anyway, since they're a lot easier to debug.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Function Macro definition problem

    If this is actually C rather than C++, then you might do something like this:
    Code:
    #define INT_SWAP(x, y) do { int t = x; x = y; y = t; } while (0)
    But if I were to infer C++ from "cpp file", then std::swap as what Lindley mentioned is the way to go.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Function Macro definition problem

    Quote Originally Posted by laserlight View Post
    If this is actually C rather than C++, then you might do something like this:
    Code:
    #define INT_SWAP(x, y) do { int t = x; x = y; y = t; } while (0)
    But if I were to infer C++ from "cpp file", then std::swap as what Lindley mentioned is the way to go.
    Why do/while(0)?
    Code:
    #define INT_SWAP(x, y) { int t = x; x = y; y = t; }

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Function Macro definition problem

    Quote Originally Posted by ninja9578
    Why do/while(0)?
    Try compiling this C program:
    Code:
    #include <stdio.h>
    
    #define INT_SWAP(x, y) { int t = x; x = y; y = t; }
    
    int main(void)
    {
        int a = 1, b = 2;
    
        if (a < b)
            INT_SWAP(a, b);
        else
            puts("No swap");
    
        printf("&#37;d %d\n", a, b);
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Function Macro definition problem

    Quote Originally Posted by ninja9578 View Post
    Why do/while(0)?
    In general, do/while(0) completes the function block and enforces the scope
    Code:
    #define INT_SWAP(x, y) { int t = x; x = y; y = t; }
    If this macro is preceeded by an expression statement that requires the terminator such as if statement, the macro will fail.
    Another reason to put a pair of curly braces even for a single statement
    Code:
        // no do/while(0) required
        if (a < b)
        {
            INT_SWAP(a, b);
        }
        else
        {
            puts("No swap");
        }
    Last edited by potatoCode; October 7th, 2010 at 11:11 AM. Reason: changed the word that was wrongly worded

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Function Macro definition problem

    Quote Originally Posted by laserlight View Post
    Try compiling this C program:
    Code:
    #include <stdio.h>
    
    #define INT_SWAP(x, y) { int t = x; x = y; y = t; }
    
    int main(void)
    {
        int a = 1, b = 2;
    
        if (a < b)
            INT_SWAP(a, b);
        else
            puts("No swap");
    
        printf("%d %d\n", a, b);
        return 0;
    }
    Ah, got it. I guess I never ran into a problem like that because I use braces for even single line if statements if there is an else.

  8. #8
    Join Date
    Jun 2008
    Posts
    15

    Re: Function Macro definition problem

    Thanks, I have managed to build the code by changing the function as an inline function. But I still cannot figure out what's wrong with the previous declaration...

    regards,
    Eric

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Function Macro definition problem

    Quote Originally Posted by Ericxx
    But I still cannot figure out what's wrong with the previous declaration...
    You must have read Lindley's explanation in post #2. Is there anything about it that you would like to have clarified?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function Macro definition problem

    Well, I didn't say why you can't define global variables in header files. The reason is, if multiple cpp files include the header, then they each get "different copies" of the global. So you end up with two global variables that have the same name, and the linker chokes on that.

    There are ways around the problem, but none of them is the "right" solution in this case so I won't go into details.

  11. #11
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Function Macro definition problem

    Quote Originally Posted by laserlight View Post
    Try compiling this C program:
    Code:
    #include <stdio.h>
    
    #define INT_SWAP(x, y) { int t = x; x = y; y = t; }
    
    int main(void)
    {
        int a = 1, b = 2;
    
        if (a < b)
            INT_SWAP(a, b);
        else
            puts("No swap");
    
        printf("%d %d\n", a, b);
        return 0;
    }
    Quote Originally Posted by VS2005
    error C2181: illegal else without matching if
    Classic !
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured