Click to See Complete Forum and Search --> : macro
mickey0
May 14th, 2008, 05:13 PM
#define PRINT(STR,VAR) \
cout << STR " = " VAR << endl
int i = 10;
PRINT("i;", i);
HI, how come this macro doen't work?
souldog
May 14th, 2008, 05:19 PM
1. Don't use the Macro.
2. You forgot the semicolon, you forgot <<
#define PRINT(STR,VAR) \
cout << STR << " = " << VAR << endl;
souldog
May 14th, 2008, 05:28 PM
use a template function instead
template<typename T>
void PRINT(const std::string& name, const T value)
{
std::cout << name << " = " << value << std::endl;
}
mickey0
May 14th, 2008, 05:41 PM
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...
souldog
May 14th, 2008, 06:35 PM
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
angelorohit
May 14th, 2008, 06:49 PM
HI, how come this macro doen't work?Well, what is the output that you expect to get?
mickey0
May 14th, 2008, 06:56 PM
I expect
i: 10
The real problem is that is doesn't compile.
angelorohit
May 14th, 2008, 07:01 PM
I expect
i: 10
The real problem is that is doesn't compile.Try this:
#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.
mickey0
May 15th, 2008, 03:58 AM
nothing to do:
However the errors without sense for me) are:
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)...
ajbharani
May 15th, 2008, 04:45 AM
This works perfectly for me.
#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
ajbharani
May 15th, 2008, 04:49 AM
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
laserlight
May 15th, 2008, 04:58 AM
Why should not we use macro here?
Read Stroustrup's answer to the FAQ: So, what's wrong with using macros? (http://www.research.att.com/~bs/bs_faq2.html#macro)
ajbharani
May 15th, 2008, 05:27 AM
Thank you, laserlight! That's a nice piece of information :)
JohnW@Wessex
May 15th, 2008, 06:39 AM
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.
staticVoid
May 15th, 2008, 06:55 AM
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.
mickey0
May 15th, 2008, 08:08 AM
I doens't still work; my code now is:
#include <iostream>
#define PRINT(STR, VAR) \
std::cout<<STR<<" = "<<VAR<<std::endl;
int main (int argc, char argv[]) {
int i = 10;
PRINT("var", i);
return EXIT_SUCCESS;
}
error C2143: syntax error : missing ';' before '<<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2371: 'std::cout' : redefinition; different basic types
see declaration of 'std::cout'
error C2017: illegal escape sequence //this error is on PRINT("var",i);
ajbharani
May 15th, 2008, 08:37 AM
#define PRINT(STR, VAR) \
Please eliminate the blank space after the '\' and re-compile. It works then.
Bharani
staticVoid
May 15th, 2008, 10:46 AM
I doens't still work; my code now is:
#include <iostream>
#define PRINT(STR, VAR) \
std::cout<<STR<<" = "<<VAR<<std::endl;
int main (int argc, char argv[]) {
int i = 10;
PRINT("var", i);
return EXIT_SUCCESS;
}
error C2143: syntax error : missing ';' before '<<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2371: 'std::cout' : redefinition; different basic types
see declaration of 'std::cout'
error C2017: illegal escape sequence //this error is on PRINT("var",i);
warning: enclose VAR in () : (VAR), If you have an expression like:
PRINT("1 shift left 5: ", 1 << 5)
this will produce:
std::cout<<"1 shift left 5"<<" = "<< 1 << 5 <<std::endl;
you can see the problem. this is another reason not to use macros.
mickey0
May 15th, 2008, 05:45 PM
#define PRINT(STR, VAR) \
Please eliminate the blank space after the '\' and re-compile. It works then.
Bharani
THANKS!!!!!!!
Now it works even the version on the TC++ that is:
#define PRINT(STR, VAR) \
std::cout << STR " = " << VAR << std::endl
THis above compile and work!
It was the white space after '\'
I didn't know that!
Thanks.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.