|
-
May 14th, 2008, 05:13 PM
#1
macro
Code:
#define PRINT(STR,VAR) \
cout << STR " = " VAR << endl
int i = 10;
PRINT("i;", i);
HI, how come this macro doen't work?
-
May 14th, 2008, 05:19 PM
#2
Re: macro
1. Don't use the Macro.
2. You forgot the semicolon, you forgot <<
#define PRINT(STR,VAR) \
cout << STR << " = " << VAR << endl;
Last edited by souldog; May 14th, 2008 at 05:29 PM.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
May 14th, 2008, 05:28 PM
#3
Re: macro
use a template function instead
Code:
template<typename T>
void PRINT(const std::string& name, const T value)
{
std::cout << name << " = " << value << std::endl;
}
Last edited by souldog; May 14th, 2008 at 05:51 PM.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
May 14th, 2008, 05:41 PM
#4
Re: macro
 Originally Posted by souldog
1. Don't use the Macro.
2. You forgot the semicolon, you forgot <<
#define PRINT(STR,VAR) \
cout << STR << " = " << VAR << endl;
it doens't work; I copied this example in TC++, chapter "the C in c++";
at the moment I'd like learn something about macro...
-
May 14th, 2008, 06:35 PM
#5
Re: macro
Well it is impossible to know without you giving a hint as to what is not working.
Look at it this way: What you are now learning is why you should never use macros to do things like this.
Macros have there uses, this is not one of them
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
May 14th, 2008, 06:49 PM
#6
Re: macro
 Originally Posted by mickey0
HI, how come this macro doen't work?
Well, what is the output that you expect to get?
-
May 14th, 2008, 06:56 PM
#7
Re: macro
I expect
i: 10
The real problem is that is doesn't compile.
-
May 14th, 2008, 07:01 PM
#8
Re: macro
 Originally Posted by mickey0
I expect
i: 10
The real problem is that is doesn't compile.
Try this:
Code:
#include <iostream>
#define PRINT(STR,VAR) \
std::cout << #STR << " " << VAR << std::endl
int main()
{
int i = 10;
PRINT(i;, i);
return 0;
}
The trick is where I put the # in front of STR in the macro.
-
May 15th, 2008, 03:58 AM
#9
Re: macro
nothing to do:
However the errors without sense for me) are:
Code:
error C2143: syntax error : missing ';' before '<<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2014: preprocessor command must start as first nonwhite space
and other error in all the program (that I haven't if I comment that macro)...
-
May 15th, 2008, 04:45 AM
#10
Re: macro
This works perfectly for me.
Code:
#include<iostream>
#define PRINT(STR,VAR) \
std::cout<<STR<<"="<<VAR<<std::endl;
int main()
{
PRINT("Variable",10)
return 0;
}
I tried with VC++6.0 and VS2005
Bharani
Rate the posts which you find useful
-
May 15th, 2008, 04:49 AM
#11
Re: macro
 Originally Posted by souldog
Well it is impossible to know without you giving a hint as to what is not working.
Look at it this way: What you are now learning is why you should never use macros to do things like this.
Macros have there uses, this is not one of them
Why should not we use macro here?
Bharani
Rate the posts which you find useful
-
May 15th, 2008, 04:58 AM
#12
Re: macro
Why should not we use macro here?
Read Stroustrup's answer to the FAQ: So, what's wrong with using macros?
-
May 15th, 2008, 05:27 AM
#13
Re: macro
Thank you, laserlight! That's a nice piece of information
Rate the posts which you find useful
-
May 15th, 2008, 06:39 AM
#14
Re: macro
One reason why I hate macro 'functions' is that it is source code created at compile time and as such it can be very difficult to figure out what code you're actually running!
On a related note, I think template meta-programming potentially suffers same problems as it is also source code created at compile time. A complicated template meta-program can be difficult to decypher.
-
May 15th, 2008, 06:55 AM
#15
Re: macro
just replace the macro name with the text you have defined after the macro(that's all the compiler does) and you'll see what's wrong eg.
#define PRINT(STR,VAR) \
cout << STR " = " VAR << endl
int i = 10;
PRINT("i;", i);
replaced by:
cout << "i;" " = " 10 << endl
you can see that there should be << inbetween i; and = and there should a semi-colon after endl. If you MUST use a macro use:
#define PRINT(STR, VAR) \
std::cout << STR << " = " << (VAR) << std::endl;
also, you should include the namespace std:: before cout and endl unless you are sure "using namespace std;" has been called before the macro.
Last edited by staticVoid; May 15th, 2008 at 06:59 AM.
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
|