Expected unqualified-id - WProgram.h

For a particular piece of code, I need to include the WProgram library. However, with this I start getting the following errors throughout the rest of my code:

product.ino: In function 'void loop()':
../wiring/inc/spark_wiring_arduino_constants.h:72:16: error: expected unqualified-id before '(' token
#define abs(x) ((x)>0?(x):-(x))
^
product.ino:248:20: note: in expansion of macro 'abs

248 has the following line of code:

X2 = std::abs(X-X1);

Any thoughts on this? I do make use of the abs() function a fair amount, so it's impacting my code quite a bit.

Is there a particular reason you need to include WProgram.h? That will enable extra Arduino compatibility mode. In most cases it’s better to include Particle.h instead of WProgram.h if you don’t need extended Arduino compatibility.

The problem with abs() is that Arduino declares it as a macro. This breaks compatibility with std::abs, using the abs function in the Standard C++ library.

5 Likes

It was in a particular library that I have been using to improve the accuracy/reliability of my ultrasonic sensor. I guess the author used WProgram.h as he designed it as a library compatible on a large range of microcontrollers,

Thanks for explaining it to me, I simply replaced WProgram.h with particle.h and now everything compiles fine :slight_smile:

3 Likes