[RESOLVED] Expected unqualified-id before 'else' error

I'm stuck on this error, have no clue why it's happening, syntax seems to be correct, all braces are in right places...

#define fWrite(pin, val) if (val) switch (pin) {     \
    case 0: PORTD |= 1<<0; break;       \
    case 1: PORTD |= 1<<1; break;       \
    case 2: PORTD |= 1<<2; break;       \
    case 3: PORTD |= 1<<3; break;       \
    case 4: PORTD |= 1<<4; break;       \
    case 5: PORTD |= 1<<5; break;       \
    case 6: PORTD |= 1<<6; break;       \
    case 7: PORTD |= 1<<7; break;       \
    case 8: PORTB |= 1<<0; break;       \
    case 9: PORTB |= 1<<1; break;       \
    case 10: PORTB |= 1<<2; break;      \
    case 11: PORTB |= 1<<3; break;      \
    case 12: PORTB |= 1<<4; break;      \
    case 13: PORTB |= 1<<5; break;      \
    } else switch (pin) {               \
    case 0: PORTD &= ~(1<<0); break;    \
    case 1: PORTD &= ~(1<<1); break;    \
    case 2: PORTD &= ~(1<<2); break;    \
    case 3: PORTD &= ~(1<<3); break;    \
    case 4: PORTD &= ~(1<<4); break;    \
    case 5: PORTD &= ~(1<<5); break;    \
    case 6: PORTD &= ~(1<<6); break;    \
    case 7: PORTD &= ~(1<<7); break;    \
    case 8: PORTB &= ~(1<<0); break;    \
    case 9: PORTB &= ~(1<<1); break;    \
    case 10: PORTB &= ~(1<<2); break;   \
    case 11: PORTB &= ~(1<<3); break;   \
    case 12: PORTB &= ~(1<<4); break;   \
    case 13: PORTB &= ~(1<<5); break;   \
    }

Get rid of all the single "" in the file.

I can't get that to compile because the IDE goes into a loop so I can't test anything.
However, you haven't bracketed the if statement properly. When there's only one statement it should be:

 if () statement1; else statement2;

but you have

 if () statement1 else statement2

Best thing to do would be to add brackets to the if so that it has this structure:

 if () {statement1} else {statement2}

and then the semicolon isn't required.

Pete

Is this code within your sketch?
All the # statements need to be at the top of the sketch.
Is intended to be a function? macro? class? something else?

#define fWrite(pin, val) if (val) switch (pin) { \

Best thing to do would be to add brackets to the if so that it has this structure:
Code:
if () {statement1} else {statement2}
and then the semicolon isn't required.

What? So the code below should compile?

void setup() {

  int a;
 if (true) {
   a = 5
 } else {
   a = 6
 }
}

void loop() {
}

This is news to me.

CrossRoads:
All the # statements need to be at the top of the sketch.

Nope, #defines can show up anywhere, but they will only be visible to code below it in the file, which can be problematic on the arduino since it munges files together.

Sorry about confusion...
It's a macro inside LED_2416.cpp (matrix driver library). I didn't write it.
It supposed to translate Arduino pin numbers into hardware pins thus making digitawrites 100x faster. And it works, I just couldn't get this Macro to compile for some reason.
"/" are meant to be carryover to next line, without them whole thing will need to be written on single line.
And basically syntax is correct. If I make into a function (i.e. "void fWriteB(byte pin, byte val) ") it compiles, but doesn't give me fast pin write effect for some reason. C++ macros apparently have their own syntax, and this is too advanced for me at this point :frowning:
Fortunately I finally found substitution macro that works:

//Atmega328 Version of fastWrite - for pins 0-13
#define fWrite(_pin_, _state_) ( _pin_ < 8 ? (_state_ ?  PORTD |= 1 << _pin_ : \
PORTD &= ~(1 << _pin_ )) : (_state_ ?  PORTB |= 1 << (_pin_ -8) : PORTB &= ~(1 << (_pin_ -8)  )))

P.S. I'm writing sketch for my wearable LED display (w/ dual 24x16 LEDs) that I plan to show off at Maker's fair. It was working fine with my ATMega1284p board, but that was too big to fit inside the case, so I'm rewriting it to run off small ATMega328 board. I was stuck on this fastwrite for a while, thus my question. But it's good now! Thank you all! :slight_smile:

KeithRB:

CrossRoads:
All the # statements need to be at the top of the sketch.

Nope, #defines can show up anywhere, but they will only be visible to code below it in the file, which can be problematic on the arduino since it munges files together.

I think you both right. In main sketch it should be at the top, but if you including external library (like in my case), that library can have it anywhere.

This is news to me.

Me too!
I was thinking specifically of the switch statement in the OP's macro but generalized it incorrectly.
This structure

if () switch () {} else switch () { }

doesn't need the terminating semicolon.

Pete

I don't know why you would use a macro to implement a long-winded function like that. Your program will include that code at every instance in your code where you invoke your pinWrite( ) pseudo function. If you called that function more than a few times, you would very quickly run out of program memory. And, if execution speed is important, as you seem to think it is, there are several much faster ways to identify the correct pin, than those long switch statements.