Help with #define

  • Thread starter Thread starter ewpatton
  • Start date Start date
E

ewpatton

I have two preprocessing macros defined in my program:

#define SetBooleanAttribute(str, attr) else if(key->Equals(str)) \
##attr## = (settings->Attributes["value"]->Value=="true" ? true :
false);
#define SetStringAttribute(str, attr) else if(key->Equals(str)) \
##attr## = settings->Attributes["value"]->Value;

Then in my code I have the following:

1: if(key->Equals("version"))
2: this->version = settings->Attributes["value"]->Value;
3: SetStringAttribute("mode", this->mode);
4: SetStringAttribute("path", this->path);
5: else
6: MessageBox::Show("Unknown setting.", "Unknown",
MessageBoxButtons::OK);

But when I try to compile my program, the compiler states that I have
an invalid else without matching if on line 4, yet it seemed to handle
line 3 just fine.

What am I doing wrong?



Evan
 
You have a semicolon in your macro expansion and one coming from the main
line of code. This close the if statement (without brace onlu one
instruction can be present). Remove the semicolon in the macro definition.

Your expansion look like:

if (cond) statement ;
else if (cond) statement ; ; <-- this is a second null statement after your
if (closing it)
else if (cond) statement ; ;
 
Oye, that was dumb of me. Thank you.

Evan

You have a semicolon in your macro expansion and one coming from the main
line of code. This close the if statement (without brace onlu one
instruction can be present). Remove the semicolon in the macro definition.

Your expansion look like:

if (cond) statement ;
else if (cond) statement ; ; <-- this is a second null statement after your
if (closing it)
else if (cond) statement ; ;


I have two preprocessing macros defined in my program:
#define SetBooleanAttribute(str, attr) else if(key->Equals(str)) \
##attr## = (settings->Attributes["value"]->Value=="true" ? true :
false);
#define SetStringAttribute(str, attr) else if(key->Equals(str)) \
##attr## = settings->Attributes["value"]->Value;
Then in my code I have the following:
1: if(key->Equals("version"))
2: this->version = settings->Attributes["value"]->Value;
3: SetStringAttribute("mode", this->mode);
4: SetStringAttribute("path", this->path);
5: else
6: MessageBox::Show("Unknown setting.", "Unknown",
MessageBoxButtons::OK);
But when I try to compile my program, the compiler states that I have
an invalid else without matching if on line 4, yet it seemed to handle
line 3 just fine.
What am I doing wrong?
 
Back
Top