HI, how come this macro doen't work?Code:#define PRINT(STR,VAR) \
cout << STR " = " VAR << endl
int i = 10;
PRINT("i;", i);
Printable View
HI, how come this macro doen't work?Code:#define PRINT(STR,VAR) \
cout << STR " = " VAR << endl
int i = 10;
PRINT("i;", i);
1. Don't use the Macro.
2. You forgot the semicolon, you forgot <<
#define PRINT(STR,VAR) \
cout << STR << " = " << VAR << endl;
use a template function instead
Code:template<typename T>
void PRINT(const std::string& name, const T value)
{
std::cout << name << " = " << value << std::endl;
}
it doens't work; I copied this example in TC++, chapter "the C in c++";Quote:
Originally Posted by souldog
at the moment I'd like learn something about 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
Well, what is the output that you expect to get?Quote:
Originally Posted by mickey0
I expect
i: 10
The real problem is that is doesn't compile.
Try this:Quote:
Originally Posted by mickey0
The trick is where I put the # in front of STR in the macro.Code:#include <iostream>
#define PRINT(STR,VAR) \
std::cout << #STR << " " << VAR << std::endl
int main()
{
int i = 10;
PRINT(i;, i);
return 0;
}
nothing to do:
However the errors without sense for me) are:
and other error in all the program (that I haven't if I comment that macro)...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
This works perfectly for me.
I tried with VC++6.0 and VS2005Code:#include<iostream>
#define PRINT(STR,VAR) \
std::cout<<STR<<"="<<VAR<<std::endl;
int main()
{
PRINT("Variable",10)
return 0;
}
Bharani
Why should not we use macro here?Quote:
Originally Posted by souldog
Bharani
Read Stroustrup's answer to the FAQ: So, what's wrong with using macros?Quote:
Why should not we use macro here?
Thank you, laserlight! That's a nice piece of information :)
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.
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.