CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: macro

  1. #1
    Join Date
    Jul 2007
    Posts
    273

    macro

    Code:
    #define PRINT(STR,VAR) \ 
            cout << STR " = "  VAR << endl
    int i = 10;
    PRINT("i;", i);
    HI, how come this macro doen't work?

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  3. #3
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  4. #4
    Join Date
    Jul 2007
    Posts
    273

    Re: macro

    Quote 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...

  5. #5
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  6. #6
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: macro

    Quote Originally Posted by mickey0
    HI, how come this macro doen't work?
    Well, what is the output that you expect to get?
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  7. #7
    Join Date
    Jul 2007
    Posts
    273

    Re: macro

    I expect
    i: 10

    The real problem is that is doesn't compile.

  8. #8
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: macro

    Quote 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.
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  9. #9
    Join Date
    Jul 2007
    Posts
    273

    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)...

  10. #10
    Join Date
    Jan 2008
    Location
    India
    Posts
    408

    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

  11. #11
    Join Date
    Jan 2008
    Location
    India
    Posts
    408

    Re: macro

    Quote 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

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

    Re: macro

    Why should not we use macro here?
    Read Stroustrup's answer to the FAQ: So, what's wrong with using macros?
    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

  13. #13
    Join Date
    Jan 2008
    Location
    India
    Posts
    408

    Re: macro

    Thank you, laserlight! That's a nice piece of information
    Rate the posts which you find useful

  14. #14
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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.

  15. #15
    Join Date
    Oct 2007
    Location
    Scotland
    Posts
    137

    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.

Page 1 of 2 12 LastLast

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