Problems with passing a instance of a class as a parameter

Hi,

I'm currently trying to control multiple 28bj-48 using a MCP23017.
For that, I have to rewrite the Stepper Library to use the MCP23017 digitalWrite alternative function instead of the normal digitalWrite one. But I'm struggeling with passing a Instance of the MCP23017 class over to the rewritten Stepper Library. (I put the MCP23017 library files into the modified Stepper library folder, so it "knows" what a MCP23017 class is, I'm sure there is a different way, but this is how it works right now)

This is how I first instantiate a Instance of the MCP23017 class and pass that instance over to the Stepper library (in my .ino file):

MCP23017 MCP = MCP23017(MCP_ADDRESS);

Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4, false, MCP);

Here is the code of the function in the stepper library(at least the important initialisation part:

Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4, bool isASide, MCP23017& MCP)
{
  this->step_number = 0;    // which step the motor is on
  this->direction = 0;      // motor direction
  this->last_step_time = 0; // time stamp in us of the last step taken
  this->number_of_steps = number_of_steps; // total number of steps for this motor

  // Arduino pins for the motor control connection:
  this->motor_pin_1 = motor_pin_1;
  this->motor_pin_2 = motor_pin_2;
  this->motor_pin_3 = motor_pin_3;
  this->motor_pin_4 = motor_pin_4;

  this->isASide = isASide;
  this->_MCP = MCP;

  // When there are 4 pins, set the others to 0:


  // pin_count is used by the stepMotor() method:
  this->pin_count = 4;
}

and here is the .h stepper code:

class Stepper {
  public:
    // constructors:
    Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4, bool isASide, MCP23017& MCP);

    // speed setter method:
    void setSpeed(long whatSpeed);

    // mover method:
    void step(int number_of_steps);

    int version(void);

  private:
    void stepMotor(int this_step);

    int direction;            // Direction of rotation
    unsigned long step_delay; // delay between steps, in ms, based on speed
    int number_of_steps;      // total number of steps this motor can take
    int pin_count;            // how many pins are in use.
    int step_number;          // which step the motor is on

    // motor pin numbers:
    MCP23017 _MCP;
    int motor_pin_1;
    int motor_pin_2;
    int motor_pin_3;
    int motor_pin_4;

    bool isASide;



    unsigned long last_step_time; // time stamp in us of when the last step was taken
};

When I try to compile the code, I get this error:

c:\Users\finnu\Documents\Arduino\libraries\Stepper_MCP23017\src\Stepper_MCP23017.cpp: In constructor 'Stepper::Stepper(int, int, int, int, int, bool, MCP23017&)':
c:\Users\finnu\Documents\Arduino\libraries\Stepper_MCP23017\src\Stepper_MCP23017.cpp:86:134: error: no matching function for call to 'MCP23017::MCP23017()'
   86 | Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4, bool isASide, MCP23017& MCP)
      |                                                                                                                                      ^
In file included from c:\Users\finnu\Documents\Arduino\libraries\Stepper_MCP23017\src\Stepper_MCP23017.h:81,
                 from c:\Users\finnu\Documents\Arduino\libraries\Stepper_MCP23017\src\Stepper_MCP23017.cpp:79:
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:74:9: note: candidate: 'MCP23017::MCP23017(SPIClass*, uint8_t, uint8_t, uint8_t)'
   74 |         MCP23017(SPIClass *s, uint8_t cs, uint8_t rp, uint8_t addr) : _spi{s}, SPI_Address{addr}, resetPin{rp}, csPin{cs}, useSPI{true} {}
      |         ^~~~~~~~
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:74:9: note:   candidate expects 4 arguments, 0 provided
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:73:9: note: candidate: 'MCP23017::MCP23017(uint8_t, uint8_t, uint8_t)'
   73 |         MCP23017(uint8_t cs, uint8_t rp, uint8_t addr) : _spi{&SPI}, SPI_Address{addr}, resetPin{rp}, csPin{cs}, useSPI{true} {}
      |         ^~~~~~~~
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:73:9: note:   candidate expects 3 arguments, 0 provided
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:72:9: note: candidate: 'MCP23017::MCP23017(TwoWire*, uint8_t, uint8_t)'
   72 |         MCP23017(TwoWire *w, uint8_t addr, uint8_t rp = 99) : _wire{w}, I2C_Address{addr}, resetPin{rp}, useSPI{false} {}
      |         ^~~~~~~~
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:72:9: note:   candidate expects 3 arguments, 0 provided
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:71:9: note: candidate: 'MCP23017::MCP23017(uint8_t, uint8_t)'
   71 |         MCP23017(uint8_t addr, uint8_t rp = 99) : _wire{&Wire}, I2C_Address{addr}, resetPin{rp}, useSPI{false} {}
      |         ^~~~~~~~
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:71:9: note:   candidate expects 2 arguments, 0 provided
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:42:7: note: candidate: 'constexpr MCP23017::MCP23017(const MCP23017&)'
   42 | class MCP23017{
      |       ^~~~~~~~
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:42:7: note:   candidate expects 1 argument, 0 provided
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:42:7: note: candidate: 'constexpr MCP23017::MCP23017(MCP23017&&)'
c:\Users\finnu\Documents\Arduino\libraries\MCP23017_WE\src/MCP23017.h:42:7: note:   candidate expects 1 argument, 0 provided

exit status 1

Compilation error: exit status 1

Why is this? Any help is appreciated.

Your class just could use a pointer to the instance, not a full object.

    // motor pin numbers:
    MCP23017 * _MCP;

Change the constructor to also use a pointer

and in the code use _MCP-> instead of _MCP.

Since you are re-writing the stepper library anyway, why not just include the MCP23017 class in the Stepper library rather than at top level in the .ino? That would eliminate the need to pass anything. You could add a function to set the I2C address.

You should probably re-name the class (e,g McpStepper or whatever) to avoid confusion.

Or define _MCP as reference as the parameter passed to the constructor:

// motor pin numbers:
MCP23017& _MCP;

Thank you for all your answers! I decided to try BitSeekers idea and implemented the MCP23017 class in the new Stepper library. Works great