[SOLVED] Compile error when extending library class

Hi,
I just can't get this right :cold_sweat:. I want to extend the Arduino library Servo class. I have three tabs in in Arduino 1.0 IDE; 1) the sketch using my class Actuator 2) the header actuator.h file and 3) the body actuator.cpp file. Problem is that compilation fails and compiler returns

In file included from actuator.cpp:1:
actuator.h:6: error: expected class-name before '{' token

What's wrong with my code?

The skecth

#include "actuator.h" 

Actuator myservo;  // create Actuator object to control a servo 
int pos = 1500;

void setup() 
{ 
  Serial.begin(9600);
  myservo.attach(6);  // attaches the servo on pin 6
} 

void loop()
{ 
  if ( Serial.available() ) {
    pos = Serial.parseInt(); 
    myservo.set(pos);  
  }
  delay(15);
}

actuator.h

#ifndef ACTUATOR_H
#define ACTUATOR_H
#include "Arduino.h" 
#include <Servo.h> 

class Actuator : 
public Servo {
public :
  String a_string;
  void set(int);
  void attach(int);
};

#endif

actuator.cpp

#include "actuator.h"

void Actuator::set(int i) {
  // set position
}
void Actuator::attach(int i) {
  // attach pin
}

I got this error:

In file included from actuator.cpp:1:0:
actuator.h:4:20: fatal error: Servo.h: No such file or directory

so I added #include <Servo.h> to the main sketch (after the actuator.h include) and it compiled fine.

Looking at it again, the compiler doesn't think Servo is a class because the IDE didn't link in the Servo class. Including it in the main sketch makes the IDE give the appropriate options to the compiler to make it see Servo

Thanks for your reply! Right, adding #include <servo.h> to the sketch makes it compile successfully for me too.
But isn't that a bit strange? It is not very intuitive, becuase I'm not using the Servo class in the sketch directly.
Am I doing something which is really supposed to be done in another way? E.g. making an actuator library instead?

Ah yes, we've been down that path before. Think of it as documentation. In the main sketch you put includes in for the libraries you are using (even indirectly). Then the compiler helpfully includes them.

Allright, I guess that is simply they way it works so answer accepted.
I searched a lot for this issue (found among aother things Redirecting) but couldn't find a solution.

Thanks!

Allright, I guess that is simply they way it works so answer accepted.

If you enable verbose mode when building, then things become clearer.

The IDE copies files to a temporary directory where it performs the build. What gets copied? Well, all the files in the sketch directory get copied (the sketch, the actuator.cpp file, and the actuator.h file).

Then, the sketch is parsed, looking for header files that also need to be copied (as well as the related cpp file). As originally defined, the sketch does not reference Servo.h, so Servo.h is not copied (nor is Servo.cpp).

So, when the compiler tries to include Servo.h in actuator.h, it can't, because the file is not available, in the temporary directory).

Making it explicit, to the sketch, that Servo.h is needed causes the build process to copy Servo.h (and Servo.cpp) to the temporary directory, so that they are available for other files to include, too.

You'll notice that only files with the .ino (or .pde) extension are scanned for include files. This is by design, for simplicity for one thing and so that a library can't include another library that includes another library that includes another library. Making the sketch include all the libraries needed makes it very clear what libraries a sketch will need.

If one library were free to include another that included another, without making it explicitly obvious that this was happening, it could take many iterations of downloading libraries and head scratching getting the (sample) code to compile.

PaulS - Thanks for the tip and great explanation! That makes sense. :slight_smile: