2

I'm breaking my head trying to use Servo.h inside of a library I made. The compiler consistently gives me the same error, as if it does not recognize the class, which is included in my library.

I am trying to make a new class, one of it's properties is a Servo object, which I should pass in the constructor. No matter how I try, I keep receiving the same error message when trying to compile my sketch:

In file included from /home/nezah/Arduino/My sketches/CameraShutter/CameraShutter.ino:8:0: /home/nezah/Arduino/libraries/Shutter/Shutter.h:13:19: error: expected ')' before '*' token Shutter(Servo *servo);

It seems that the include statement is ok, as I get a different message if I mess it to go wrong or remove it completely. I already tried to change "" for <> and even copied the source in a folder and use the full path. No change as far as I don't mess it (on purpose). I already read this.

I also tried to pass it as a pointer, using Shutter(Servo* servo), Shutter(Servo *servo) and Shutter(Servo& servo). Same error message.

In some arduino.cc forum I read that I rather forget it and avoid using libraries inside other libraries, but I bet this is possible.

Is there anybody so kind as to give me some hints on how to do this?

I leave you part of my .h and .cpp of the library I'm trying to write (which, by the way, turns a servo into a physical button presser but with burst capability).

 /*
 * Shutter.h - Library to make a photocamera shutter out of a servo
 *   alternatively it could press any physical button with a servo.
 */

#ifndef Shutter
#define Shutter
#include "Servo.h"

class Shutter {
  public:
    Shutter(Servo *servo);
    Servo getServo();

    void shut();
  private:
    Servo _servo;        
}

#endif

And here is my .cpp:

/*
  Shutter.cpp - Library for flashing Shutter code.
  Created by David A. Mellis, November 2, 2007.
  Released into the public domain.
*/

#include <Arduino.h>
#include "Servo.h"
#include "Shutter.h"

Shutter::Shutter(Servo *servo) {
    _servo = servo;
}

NOTE: If I remove some code and take away the "Servo" part of the constructor, I get an error message on the "getServo()" code. The problem seems to be that the compiler does not recognize "Servo" as a valid type inside my library.

Thanks in advance!

Community
  • 1
  • 1
elnezah
  • 425
  • 5
  • 17

2 Answers2

2

In the constructor of your class you are passing a pointer of the type Servo, so you must store that value in another pointer. To do this you must change:

*.h

#ifndef SHUTTER_H
#define SHUTTER_H
#include "Servo.h"

class Shutter {
  public:
    Shutter(Servo *servo);
    Servo *getServo() const;

    void shut();
  private:
    Servo *_servo;        
}

#endif

*.cpp

Shutter::Shutter(Servo *servo) {
    _servo = servo;
}


Servo *Shutter::getServo() const
{
    return _servo;
}

Use:

Servo servo;
Shutter shuter(&servo)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • First of all, thanks a lot for your help! I tried what you told me, but still get the same error message: `In file included from /home/nezah/Arduino/My sketches/CameraShutter/CameraShutter.ino:1:0: /home/nezah/Arduino/libraries/Shutter/Shutter.h:12:19: error: expected ')' before '*' token Shutter(Servo *servo);`. I would ask: is there something I have to do to the IDE to include the library Servo? I have used it in sketches before, but not in libraries. – elnezah May 04 '17 at 18:39
  • Your answer helped me to correct the code, which was a mess, but didn't really solve the problem. Anyway gave me the hint to find the solution, I think. Thanks again!! – elnezah May 04 '17 at 19:03
  • You could share your complete code (* .ino, * .h, * .cpp) via github, dropbox or similar. – eyllanesc May 04 '17 at 19:07
  • After I finished and commented the code of the library, I uploaded it to GitHub: https://github.com/elnezah/Shutter – elnezah May 05 '17 at 13:11
0

Looks like the problem was that the class name and the #ifndef marker was the same, so there was a name conflict somehow. It is well explained in this thread: How to properly use a header file to be a complete class?

After fixing this, it compiled well.

Community
  • 1
  • 1
elnezah
  • 425
  • 5
  • 17