-
October 7th, 2010, 09:35 AM
#1
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
-
October 7th, 2010, 09:38 AM
#2
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.
-
October 7th, 2010, 09:50 AM
#3
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.
-
October 7th, 2010, 10:16 AM
#4
Re: Function Macro definition problem
Originally Posted by laserlight
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; }
-
October 7th, 2010, 10:27 AM
#5
Re: Function Macro definition problem
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("%d %d\n", a, b);
return 0;
}
-
October 7th, 2010, 11:09 AM
#6
Re: Function Macro definition problem
Originally Posted by ninja9578
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
-
October 7th, 2010, 12:23 PM
#7
Re: Function Macro definition problem
Originally Posted by laserlight
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.
-
October 8th, 2010, 11:14 AM
#8
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
-
October 8th, 2010, 11:25 AM
#9
Re: Function Macro definition problem
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?
-
October 8th, 2010, 12:30 PM
#10
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.
-
October 8th, 2010, 03:41 PM
#11
Re: Function Macro definition problem
Originally Posted by laserlight
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;
}
Originally Posted by VS2005
error C2181: illegal else without matching if
Classic !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|