Library Code - .cpp and .h

Hi All,

I thought that I would have a go at developing my own library, so that a large section of code is hidden away in a library.

But I am getting an error messages when I try and compile it:-

motor.h:16:24: warning: ISO C++ forbids declaration of 'init' with no type [-fpermissive]
init(int, int, int);

motor.cpp:1:0:
H:\Arduino\libraries\Motor\motor.h:16:24: warning: ISO C++ forbids declaration of 'init' with no type [-fpermissive]
init(int, int, int);

motor.cpp:5:1: error: expected initializer before 'void'
void motor::init(int pwm, int in1, int in2) {

motor.cpp:29:23: error: no 'void motor::forward()' member function declared in class 'motor'
void motor::forward() {
^

motor.cpp:33:20: error: no 'void motor::back()' member function declared in class 'motor'
void motor::back() {

motor.cpp:37:1: error: expected declaration before '}' token
};
^

Here is the code that I have in the motor.h file

#ifndef MOTOR_H
#define MOTOR_H
#include <Arduino.h>

  struct motor{
    int pwmPin;
    int inPin1;
    int inPin2;

  public: 
    motor( int, int, int);
    void setDirection(boolean, boolean);
    void stop();
    void speed(int);
  private:
     init(int, int, int);
  };
#endif

And here is the code for the motor.cpp file

#include "motor.h"

motor:: motor(int pwmPin, int inPin1, int inPin2)
  
void motor::init(int pwm, int in1, int in2) {
    pwmPin = pwm;
    inPin1 = in1;
    inPin2 = in2;
    pinMode(inPin1, OUTPUT);
    pinMode(inPin2, OUTPUT);
    stop();
  }
  void motor::setDirection(boolean in1, boolean in2) {
    digitalWrite(inPin1, in1);
    digitalWrite(inPin2, in2);
  }
  void motor::stop() {
    digitalWrite(inPin1, LOW);
    digitalWrite(inPin1, LOW);
    speed(0);
  }
  void motor::speed(int aSpeed) {
    if (aSpeed >= 0) {
      analogWrite(pwmPin, aSpeed);
    }
  }
  // The following functions are not used here
  // but could be useful in future
  void motor::forward() {
    digitalWrite(inPin1, HIGH);
    digitalWrite(inPin1, LOW);
  }
  void motor::back() {
    digitalWrite(inPin1, LOW);
    digitalWrite(inPin1, HIGH);
  }
};

I have looked at several different posts with the same error, but I can't seem to see why I am getting this error. I know it is the syntax of the motor definition, but more than that, I'm not 100% sure.

Never written a library before......

Try these codes and compare with yours to see the differences.

motor.h

#ifndef MOTOR_H
#define MOTOR_H
#include <Arduino.h>

struct motor {
    int pwmPin;
    int inPin1;
    int inPin2;

  public:
    motor( int pwmPin, int inPin1, int inPin2);
    void setDirection(boolean, boolean);
    void stop();
    void speed(int);
    void motor::forward();
    void motor::back();
  private:
    void init(int pwm, int in1, int in2);
};
#endif

motor.cpp

#include "motor.h"

motor:: motor(int pwmPin, int inPin1, int inPin2);

void motor::init(int pwm, int in1, int in2) {
  pwmPin = pwm;
  inPin1 = in1;
  inPin2 = in2;
  pinMode(inPin1, OUTPUT);
  pinMode(inPin2, OUTPUT);
  stop();
}

void motor::setDirection(boolean in1, boolean in2) {
  digitalWrite(inPin1, in1);
  digitalWrite(inPin2, in2);
}

void motor::stop() {
  digitalWrite(inPin1, LOW);
  digitalWrite(inPin1, LOW);
  speed(0);
}

void motor::speed(int aSpeed) {
  if (aSpeed >= 0) {
    analogWrite(pwmPin, aSpeed);
  }
}

// The following functions are not used here
// but could be useful in future
void motor::forward() {
  digitalWrite(inPin1, HIGH);
  digitalWrite(inPin1, LOW);
}

void motor::back() {
  digitalWrite(inPin1, LOW);
  digitalWrite(inPin1, HIGH);
}


Thanks for that.

So I had missed out "Void" in front of "init" in motor.h?

I've changed that and now I am getting the following when I compile:-

H:\Arduino\libraries\Motor\motor.cpp:3:49: warning: declaration of 'motor::motor(int, int, int)' outside of class is not definition [-fpermissive]
motor:: motor(int pwmPin, int inPin1, int inPin2);
^
H:\Arduino\libraries\Motor\motor.cpp:33:6: error: prototype for 'void motor::forward()' does not match any in class 'motor'
void motor::forward() {
^~~~~
In file included from H:\Arduino\libraries\Motor\motor.cpp:1:0:
H:\Arduino\libraries\Motor\motor.h:15:10: error: candidate is: void motor::forward(boolean, boolean)
void forward(boolean, boolean);
^~~~~~~
H:\Arduino\libraries\Motor\motor.cpp:38:6: error: prototype for 'void motor::back()' does not match any in class 'motor'
void motor::back() {
^~~~~
In file included from H:\Arduino\libraries\Motor\motor.cpp:1:0:
H:\Arduino\libraries\Motor\motor.h:16:10: error: candidate is: void motor::back(boolean, boolean)
void back(boolean, boolean);
^~~~

exit status 1

Compilation error: exit status 1

Any ideas please?

Please compare everything.

It wasn't just the "void" that you forgot.

You defined the types of variables but did not inform which variables.

See here:

what class is motor declared in? (what is public in reference to)?

thinking it gets optimized away..
tried creating object in setup reveals errors..
made some changes until it compiled..

Revised simm..

good luck.. ~q

That's permissible in a function's prototype, but not it's implementation.

This one:

That function needs a return type.

where is the return type specified for init() ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.