[Resolved] Compiler error: expected unqualified-id before 'else'

I’m getting an interesting compiler error. Here it is cut down to the minimum.

Compile and you get an error on the else if(bDown) line. bDown can be static or global with the same result. I can get around it, but it’s an odd one.

bool test()
{
    return false;
}

void setup()
{

}

void loop()
{
    static bool bDown = false;

    if(test())
    {
        bDown = true;
    }
    else if(bDown)
    {
        bDown = false;
    }
}

This appears to be some kind of Spark compiler issue. Compiling it online (http://ideone.com/Fya9AC) results in no issue.

Hmm, sometimes the pre-processor gets confused, throw this at the top and you should be fine:

#pragma SPARK_NO_PREPROCESSOR

:slight_smile:

Thanks,
David

3 Likes

Hey, it looks like the original problem has been resolved so I’ve updated the topic to reflection the resolution.

However, If anyone feels that the problem still hasn’t been fixed feel free to continue positing in this thread!

Same issue today. I get the same error on the “before else”. I tried the pragma but then I get lots of errors because defines like D4, INPUT_PULLUP etc, are not valid.

        if (door_sense_rising_edge)
        {
            digitalWrite(door_open_pin, HIGH);
        }
        else if (door_sense_falling_edge)
        {
            digitalWrite(door_open_pin, LOW);
        }

You need to include a file to get all those things like D4 and INPUT_PULLUP back:

#pragma SPARK_NO_PREPROCESSOR
#include "application.h"

After this you are writing straight-up C/C++ so you need function prototypes etc.

@bko I suppose that’s fine for a temporary work-around (although just dropping the else clause is also a work-around) I would hope that the Spark people will fix their compiler so it reliably compiles ‘else if’. What say you Spark people?

Hi @Muskie

Just a quick clarification: the compiler in this case is the industry-standard gcc and it is working fine.

The only problem is the Spark pre-processor that lets novice coders write simpler code than the normal C/C++ rules would allow. I know you think of the compiler as being the thing that runs when you hit the button in the webIDE, but in reality there are two parts to it and only the first part has problems.

The reason your code is problematic for the pre-processor is that you are using a single value for the test of the if. If you re-write your code like this:

   if (door_sense_rising_edge==true)
        {
            digitalWrite(door_open_pin, HIGH);
        }
        else if (door_sense_falling_edge==true)
        {
            digitalWrite(door_open_pin, LOW);
        }

it will compile fine with the pre-processor.

The pre-processor does not work well with single value tests because it does not expect novices to write code that way. It is a bug for sure, but it is not some terrible bug, just a minor thing.

2 Likes

@bko thanks for clarifying that. I can indeed change my tests.