squarefrog squarefrog - 3 months ago 20
C++ Question

No matching function for call in Arduino Library

I am creating an Arduino library for use in a Sketch. It uses the Encoder Library, but when compiling I get an obtuse error:


/var/folders/jy/f8dvlhcd4vdcvtl49bk8ytwc0000gn/T/builda847c0675e0bee2f5f05581e35ae65fe.tmp/sketch/MIDIEncoder.cpp: In constructor 'MIDIEncoder::MIDIEncoder(uint8_t, uint8_t, byte, byte)':
MIDIEncoder.cpp:8: error: no matching function for call to 'Encoder::Encoder()'
MIDIEncoder::MIDIEncoder(uint8_t pinA, uint8_t pinB, byte midiChannel, byte midiCCNumber)
^
/var/folders/jy/f8dvlhcd4vdcvtl49bk8ytwc0000gn/T/builda847c0675e0bee2f5f05581e35ae65fe.tmp/sketch/MIDIEncoder.cpp:8:89: note: candidates are:
In file included from /var/folders/jy/f8dvlhcd4vdcvtl49bk8ytwc0000gn/T/builda847c0675e0bee2f5f05581e35ae65fe.tmp/sketch/MIDIEncoder.h:17:0,
from /var/folders/jy/f8dvlhcd4vdcvtl49bk8ytwc0000gn/T/builda847c0675e0bee2f5f05581e35ae65fe.tmp/sketch/MIDIEncoder.cpp:2:
/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:72:2: note: Encoder::Encoder(uint8_t, uint8_t)
Encoder(uint8_t pin1, uint8_t pin2) {
^
/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:72:2: note: candidate expects 2 arguments, 0 provided
/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:69:7: note: constexpr Encoder::Encoder(const Encoder&)
class Encoder
^
/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:69:7: note: candidate expects 1 argument, 0 provided
/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:69:7: note: constexpr Encoder::Encoder(Encoder&&)
/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:69:7: note: candidate expects 1 argument, 0 provided
no matching function for call to 'Encoder::Encoder()'


Fundamentally, I believe the compiler is telling me it can't find a constructor for the Encoder library
MIDIEncoder.cpp:8: error: no matching function for call to 'Encoder::Encoder()'
, but I'm stumped as to why. Below is the full source for my library.

MIDIEncoder.h

/*
MIDIEncoder.h
A library for creating relative MIDI CC messages from a rotary encoder.

Created by Paul Williamson, 11 August 2016.

Project source available at:
http://github.com/squarefrog/teensy-midi-encoder-box

Released into the public domain.
*/

#ifndef MIDIEncoder_h
#define MIDIEncoder_h

#include "Arduino.h"
#include <Encoder.h>

class MIDIEncoder
{
public:
MIDIEncoder(uint8_t pin1, uint8_t pin2, byte midiChannel, byte midiCCNumber);
byte channel;
byte ccNumber;

byte read();

private:
unsigned long _lastTurnedTime;
long _oldPosition;
Encoder _enc;
};

#endif


MIDIEncoder.cpp

#include "MIDIEncoder.h"
#include <Encoder.h>

const byte incrementValue = 66; // A constant for the start of increment values
const byte decrementValue = 2; // A constant for the start of decrement values

MIDIEncoder::MIDIEncoder(uint8_t pin1, uint8_t pin2, byte midiChannel, byte midiCCNumber)
{
channel = midiChannel;
ccNumber = midiCCNumber;
_enc = Encoder(pin1, pin2);
_oldPosition = -999;
_lastTurnedTime = millis();
}

byte MIDIEncoder::read()
{
long newPosition = _enc.read();

// If position hasn't changed, ignore.
if (newPosition == _oldPosition) {
return 0;
}

// If position is not divisible by 4, ignore.
if (newPosition % 4 != 0) {
return 0;
}

unsigned long delta = millis() - _lastTurnedTime;
byte offset = 0;

// Apply crude acceleration
if (delta < 100) offset = 4;
if (delta > 99 && delta < 180) offset = 2;
if (delta > 179 && delta <= 250) offset = 1;

_lastTurnedTime = millis();

// Return MIDI CC value
if (newPosition > _oldPosition) {
return incrementValue + offset;
} else {
return decrementValue + offset;
}
}





Some excellent answers below. Now that I know what to look for I found this resource which explains where I made my mistake. In particular look at number 3.

Answer

Encoder doesn't have a default constructor, and it is implicitly call here

MIDIEncoder::MIDIEncoder(uint8_t pin1, uint8_t pin2, byte midiChannel, byte midiCCNumber) 
    /*Implicit constructor call*/ 
{ 
    //...
}

You tried to call it the the constructor body, but by then "it is too late" :) What I mean is, _enc has to be already initialized when the constructor body executes, but _enc can't be default initialized, so the compiler complains.

That's what the constructor initializer list is for:

MIDIEncoder::MIDIEncoder(uint8_t pin1, uint8_t pin2, byte midiChannel, byte midiCCNumber)
    : _enc(pin1, pin2) //Calls appropriate constructor for _enc, not default
{
    //...
}