[Solved] Inconsistent error in syntax

Hey!

I’m getting the weirdest error in my code, and I can’t seem to see what I’m missing!

void clickBtn() {
  if (!activity) {
    if (safe) System.enterSafeMode();
    else if (listen) WiFi.listen();
    else startActivity();
  } else stopActivity();
}

This little snippet (lines 265-271) works just fine, while as this one:

void clickBtn() {
  if (!activity) {
    if (safe) System.enterSafeMode();
    else if (listen) {
      WiFi.listen();
    } else startActivity();
  } else stopActivity();
}

Doesn’t work and gives me this error:

xxxxxxxxxxxxx.cpp:10:1: error: expected unqualified-id before 'else'
 else if (listen);
 ^

Thoughts?

This is a bug in the Wiring preprocessor. It’s what allows Arudino shortcuts like not including forward declaration of functions. This code disables the preprocessor and compiles without error with your clickBtn unchanged:

#pragma SPARK_NO_PREPROCESSOR
#include "Particle.h"

bool activity = false;
bool safe = false;
bool listen = false;

void clickBtn(); // forward declaration

void setup() {
}

void loop() {
}

void startActivity() {
}

void stopActivity() {  
}

void clickBtn() {
  if (!activity) {
    if (safe) System.enterSafeMode();
    else if (listen) {
      WiFi.listen();
    } else startActivity();
  } else stopActivity();
}

Cool! Thanks! So I forward declared every single one of the functions and then it worked! Seems like a bit of a hassle though, any idea whether or not that bug is going to be fixed? And what ‘activated’ the bug exactly?

The bug in the preprocessor is usually triggered by if statements that do not have curly braces {} around the actions. In your case you have if (safe) System.enterSafeMode();.

If you just get in the habit of always using curly braces, I think you will find that the problem goes away without turning the preprocessor off.

Sorry, I forgot to post the alternate way to fix this: adding a set of curly brackets where the else if clause is like this:

void clickBtn() {
  if (!activity) {
    if (safe) System.enterSafeMode();
    else {
        if (listen) {
            WiFi.listen();
        } else startActivity();
    }
  } else stopActivity();
}
1 Like

But I tried that as well! Had curly brackets behind every single if statement, but it didn’t help at all, that’s what threw me off.

Wow, totally forgot to answer you, but I tried that other way you posted, and both ways worked just fine. Bit of a workaround, though, seems unnecessary! Anyways, thank you so much!

Also, that first solution you offered only worked if I forward declared every single function before setup.