Go Down

Topic: [SOLVED] Unexpected error in .h file of custom library (Read 441 times) previous topic - next topic

Jul 31, 2012, 07:33 pm Last Edit: Jul 31, 2012, 07:44 pm by wolfgang42 Reason: 1
I'm new to C++ and Arduino libraries, so I have no clue what's wrong here. I'm making a library to drive a bicolour LED, based on some Processing code I wrote.
Here's the code:

BiColorLed.h
Code: [Select]
#ifndef BiColorLED
#define BiColorLED 1

#include "WProgram.h"

class BiColorLED{
public:
BiColorLED(uint8_t ledPin1, uint8_t ledPin2);
void drive();
void setColor(uint8_t toColor);
private:
uint8_t color;
uint8_t pin1, pin2;
bool yellowRed;
};
 
#endif

BiColorLed.cpp
Code: [Select]
#include "BiColorLED.h"

BiColorLED::BiColorLED(uint8_t ledPin1, uint8_t ledPin2) {
pin1=ledPin1;
pin2=ledPin2;
color=0;
yellowRed=false;
}

void setColor(uint8_t toColor) {
color=toColor;
drive(); // Change the color
}

void drive() {
// TODO blinking goes here
if (color == 3) { // Yellow
color = (yellowRed?1:2);
yellowRed = !yellowRed;
} // Beyond this point the color is always red, green, or black
// Actual LED driving
if (color == 1) { // Red
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
} else if (color == 2) { // Green
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
} else { // Black
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
}


And, just for completeness, my sketch:
Code: [Select]
#include <BiColorLED.h>
void setup() {}
void loop() {}


The .h and .cpp files are largely based off of looking at other libraries. Now, here's the problem: when I verify the sketch, the Arduino IDE comes back with:
Code: [Select]

In file included from BareMinimum.cpp:1:0:
/home/wolf/sketchbook/libraries/BiColorLED/BiColorLED.h:6:7: error: expected identifier before numeric constant
/home/wolf/sketchbook/libraries/BiColorLED/BiColorLED.h:6:7: error: expected unqualified-id before numeric constant


As near as I can tell, this means that there's a problem between the words `class' and `BiColorLED'. This line is copied verbatim from another library with a change of class name, so I can't figure out what's wrong. Any ideas?

dxw00d

Code: [Select]
#define BiColorLED 1
...
class BiColorLED{


Becomes
Code: [Select]
class 1{
I believe.

WizenedEE

class BiColorLED{
Code: [Select]

#define BiColorLED 1

#include "WProgram.h"

class BiColorLED{


BiColorLED is 1, as defined at the top. So, after preprocessing, the compiler sees
Code: [Select]

class 1{

which is obviously a problem. That's also why use of the preprocessor should be avoided when possible (although you're using it fine in this case)

The solution is to change the #define -- people generally use the name of the file, like this:
Code: [Select]

#ifndef BICOLORLED_H
#define BICOLORLED_H
//...
#endif

Blech... so that's what it was. Thanks for your quick reply. I don't like the C preprocessor--it does things like that to me and I don't notice.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy