"Expected initializer before [...]" with my own library

I was trying to write a library of my own (nothing big just for a small project of mine) . However when I try to compile my sketch with the library included I only get the error:

expected initializer before 'stepper'

This error doesn't say much about what is wrong and from what I can tell this error mostly comes when someone has forgotten a semicolon or a swirly bracket. But I've looked through my code and I can't find what's causing this. I haven't written a library before so I just followed the arduino tutorial on the topic. Here is my code:

Hedder file

#ifndef MusicStepper_h
#define MusicStepper_h

#include "Arduino.h"

class MusicStepper {
  public:
    MusicStepper(int stepPin, int dirPin);
    void setFrequency(float freq);
    void play();
  private:
    int _stepPin;
    int _dirPin;
    long _lastPlay;
    float _freq;
    float _period;
    bool _step;
}

#endif

C++ Class file:

#include "Arduino.h"
#include "MusicStepper.h"

MusicStepper::MusicStepper(int stepPin, int dirPin) {
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);
    _stepPin = stepPin;
    _dirPin = dirPin;
    _lastPlay = 0;
    digitalWrite(dirPin, HIGH);
}

void MusicStepper::setFrequency(float freq) {
    _freq = freq;
    _period = 500000/freq;
}

void MusicStepper::play() {
    if (_freq != 0 && micros() - _lastPlay > _period) {
        digitalWrite(_stepPin, !_step);
        _step != _step;
        _lastPlay = micros();
    }
}

And finally the sketch:

#include <MusicStepper.h>

MusicStepper stepper(12, 13);

void setup() {}
void loop() {}

Whereabouts on the PC are your library files located ?

    float _freq;
    float _period;
    bool _step;
};

Detect the small change.... :wink:

First of all, add the trailing semicolon to the class definition in your MusicPlayer.h file.

Then you can start fixing the other typos/errors in your code.

Whandall:
First of all, add the trailing semicolon to the class definition in your MusicPlayer.h file.

Then you can start fixing the other typos/errors in your code.

Ohh there needs to be one semicolon at the end as well. Thanks! I'm too used to java syntax and did therefore not expect a semicolon there.

And yes I know I probably have lot's of typos. Comes with the dyslexia :smiley:
But thanks again

Your example code - that is absolutely dysfunctional but to show the syntax issues is ok -

only raises a

_step != step;

MusicStepper.cpp:21: error: 'step' was not declared in this scope

in

void MusicStepper::play() {
    if (_freq != 0 && micros() - _lastPlay > _period) {
        digitalWrite(_stepPin, !_step);
        _step != step;
        _lastPlay = micros();
    }
}

And the statement itself is strange. You create a boolean value that you don't use.

I did made a similar mistake. Probably wanted to do

_step = !_step;

Same chars, different order (and thus different meaning) :smiley:

septillion:
I did made a similar mistake. Probably wanted to do

_step = !_step;

Same chars, different order (and thus different meaning) :smiley:

Exactly what I meant! When you can compile your code it's easy to find all the buggs but thanks for the help anyway. Now it seem too work as intended

When you can compile your code it's easy to find all the buggs

Personally I find it easier to find the bugs that stop code compiling. It's the logic errors that are the difficult ones to track down.

This is a bad idea:

MusicStepper::MusicStepper(int stepPin, int dirPin) {
    pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);

You cannot be sure that the hardware is properly initialized before entering the "setup()" method. You should implement a "MusicStepper::begin()" method which initializes the pins and call that method in "setup()".

Danois90:
This is a bad idea:

MusicStepper::MusicStepper(int stepPin, int dirPin) {

pinMode(stepPin, OUTPUT);
    pinMode(dirPin, OUTPUT);




You cannot be sure that the hardware is properly initialized before entering the "setup()" method. You should implement a "MusicStepper::begin()" method which initializes the pins and call that method in "setup()".

Okay thanks. Didn't know it worked that way. I was just following the tutorial were they used pinMode as well in their constructor.

Mm, the "Hacking" part of the Arduino reference doesn't seem to be part of the reference on git... But indeed, what the Morse Library tutorial shows is bad practice. That's why a lot of libraries come with a .begin() method :slight_smile: