Using the Servo library within a cpp file

I wanna use a servo motor in a project.
My main code looks like this :

#include "MessageMatrix.h"
#include "MessageMovements.h"
#include "Movements.h"
#include <Servo.h>

#define pinServo 7
Bunch of variables...

Movements myMovements(..., pinServo, ...);

void setup() {
void loop(){

And so the .h file of the lib I wanna use looks like this:


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

class Movements
    Movements(..., int pinServo, ...);
    void foo(void);

    int _pinServo;
    Servo _myServo;


And finally, my .cpp:

#include "movements.h"
#include <math.h>
#include <Servo.h>

Servo myServo;

Movements::Movements(..., int pinServo, ...)
  _pinServo = pinServo;
  myServo.attach(_pinServo );
more unrelated stuff...

So the problem is that my servomotor doesn’t move. However it should at the begining of my main code when I do:

Movements myMovements(...; pinServo, ...);

I tryed other solutions I found on forums but still can’t get my code work.
I also tried to create a Servo object in my main and pass it as a parameter as so:
(I did the required modifications in the other files)

Movements myMovements(...; pinServo, myServo,...);

The Movement methods work perfectly fine except the one related to the servo -which I didn’t share).
I’m not at ease with objects/libs/classes so sorry if this is a newbie question.
Thank you !

Don’t put your attach in your constructor.

Ok but where should I put it ?

Ok but where should I put it ?

Traditionally, it would go in a function called "begin()" in your object and you would call the .begin() functions in setup(). The Servo library doesn't have a "begin()" because it has "attach()" which is required before the Servo object does anything.
I would make the "Movements" object inherit behavior from the Servo object:

class Movements : public Servo {

That way you would call myMovements.attach(pinNumber); in your setup(), as you would for my other Servo object. There is no need to duplicate the Servo's functions just to pass the data to the Servo's function.

Ok thank you I had a "go_home" function and I would always call it when I lauched my program so I put my.attach in it and everything works perfectly well !