Jump to content
  • Advertisement
Sign in to follow this  
matthughson

If Else in Macro Functions

This topic is 4733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey All, Just came across an interesting error. I'm attempting to make some macro functions, which contain if and if else statements. He's what it looks like:

// The macro functions
#define IF_COMP_AND_ALLOC(a_name01, a_name02, a_class)  { if(!strcmp(a_name01, a_name02)) return new a_class; }
#define ELSE_COMP_AND_ALLOC(a_name01, a_name02, a_class)  { else if(!strcmp(a_name01, a_name02)) return new a_class; }

// The actual use of the macros
IF_COMP_AND_ALLOC(a_pcClassName, "CMHBase", CMHBase);
ELSE_COMP_AND_ALLOC(a_pcClassName, "CMHBaseRend", CMHBaseRend);

However, when I compile I get this error: error C2181: illegal else without matching if So it seems the compiler doesn't recognize the connection between the two macros. Is the precompiler unable to do something like this? Matt Hughson

Share this post


Link to post
Share on other sites
Advertisement
Did not get a chance to test this but maybe:

// The macro functions
#define IF_COMP_AND_ALLOC(a_name01, a_name02, a_class) { if(!strcmp(a_name01, a_name02)) return new a_class; else}
#define ELSE_COMP_AND_ALLOC(a_name01, a_name02, a_class) {if(!strcmp(a_name01, a_name02)) return new a_class; }

// The actual use of the macros
IF_COMP_AND_ALLOC(a_pcClassName, "CMHBase", CMHBase);
ELSE_COMP_AND_ALLOC(a_pcClassName, "CMHBaseRend", CMHBaseRend);

Share this post


Link to post
Share on other sites
Hmmm... yeah this worked:

#define IF_COMP_AND_ALLOC(a_name01, a_name02, a_class)  if(!strcmp(a_name01, a_name02)) return new a_class; else 
#define ELSE_COMP_AND_ALLOC(a_name01, a_name02, a_class) if(!strcmp(a_name01, a_name02)) return new a_class;



Any explaination why?

Matt Hughson

Share this post


Link to post
Share on other sites
IF_COMP_AND_ALLOC(a_pcClassName, "CMHBase", CMHBase);
ELSE_COMP_AND_ALLOC(a_pcClassName, "CMHBaseRend", CMHBaseRend);

expands to


{
if(!strcmp(a_name01, a_name02))
return new a_class;
else
}

{
if(!strcmp(a_name01, a_name02))
return new a_class;
}


As you can see, having braces around each macro means they are in different blocks. Removing the braces, like you did later, fixes that. If you really want braces around the whole thing, try having BEGIN/END macros that expand to { and } and wrap your if-else macros in that.

Share this post


Link to post
Share on other sites
because they are in different scopes...its basically like trying this...

int main()
{
int a;

{
if(1 == 1 )
a = 2;
}
{
else if( 0 == 1 )
a = 2;
}

return(0);
}





edit: dammit! beat again! =)

moe.ron

Share this post


Link to post
Share on other sites
Also note that as that's a macro definition, you don't want to be having semi-colons after it in your code.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!