Help with multi-lined macro

This is a discussion on Help with multi-lined macro within the C++ Programming forums, part of the General Programming Boards category; I was wondering why this code isn't working. I'm trying to make a multi-line macro for use in my program. ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    4

    Help with multi-lined macro

    I was wondering why this code isn't working. I'm trying to make a multi-line macro for use in my program.
    Code:
    #ifndef DISPLAY_INFO
    #define DISPLAY_INFO
    x = 0;
    y = 0;
    while (x < 10){
    	cout << stuff[x][y];
    	++x;
    }
    if (y <10){		
    	++y;
    	}
    	if (x == 10 && y == 9){
    		break;
    	}
    	x = 0;
    }
    
    #endif

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Each line, including the #define, except the very last line, must terminate with a backslash.

    But this is C++. You should be using an inline function, not a macro. What happens if the code which calls this macro already has a variable x or y defined?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    Because that's not how you do multiline macros, you really should write a function instead.

    See Cprogramming.com - Tutorials - C Preprocessor Tricks if you want an answer on how to do multiline macros.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by brewbuck View Post
    What happens if the code which calls this macro already has a variable x or y defined?
    It doesn't matter. Macros are compile-time text substitution. It's not going to put in "x" and "y". It's going to put in "a" and "b":
    Code:
    #define FOO(x,y) ( x && y )
    ...
    FOO( a, b );
    What you're saying is something like: "But what happens if I pass a variable called 'foo' to a function that defines 'foo'? Nothing adverse happens, because functions don't care what the name of the variable you pass them is, so long as the type is correct.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by quzah View Post
    It doesn't matter. Macros are compile-time text substitution. It's not going to put in "x" and "y". It's going to put in "a" and "b":
    Code:
    #define FOO(x,y) ( x && y )
    ...
    FOO( a, b );
    What you're saying is something like: "But what happens if I pass a variable called 'foo' to a function that defines 'foo'? Nothing adverse happens, because functions don't care what the name of the variable you pass them is, so long as the type is correct.

    Quzah.
    Hmm - yes, if you are using x and y as parameters to the macro, indeed. But if you use variables inside a multiline macro, that are either required to be declared in the function which calls on the macro (x, y in this case), or that are declared by the macro itself, then you have a bit of a problem. It's actually the reverse of what brewbuck says: The code relies on x and y being declared (as integers, no less, since they are indexing a 2D array).

    In my mind, nearly all multiline macros that aren't also using __FILE__, __LINE__ or some such, should be made into functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ah, good point. Yes, in this case, the macro would be designed to be working with variables already declared. I misread or misunderstood the intent.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    4
    I ended up turning it into a function in my program, but if anyone wants to know you add a backslash to the end of each line in the macro except for the last line, as brewbuck said.
    Last edited by asofaihp; 04-13-2009 at 11:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. need help with multi line macro usage
    By Cdigitproc1 in forum C Programming
    Replies: 9
    Last Post: 04-29-2005, 09:50 AM
  5. about Makefile and Macro
    By tom_mk in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 01:07 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21