简体   繁体   English

Arduino库中没有匹配的调用函数

[英]No matching function for call in Arduino Library

I am creating an Arduino library for use in a Sketch. 我正在创建一个在Sketch中使用的Arduino库。 It uses the Encoder Library , but when compiling I get an obtuse error: 它使用了Encoder库 ,但是在编译时出现了一个钝角错误:

/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/l /var/folders/jy/f8dvlhcd4vdcvtl49bk8ytwc0000gn/T/builda847c0675e0bee2f5f05581e35ae65fe.tmp/sketch/MIDIEncoder.cpp:在构造函数``MIDIEncoder :: MIDIEncoder(uint8_t,uint8_t,Encoding:En错:字节:字节:'':)用于调用'Encoder :: Encoder()'MIDIEncoder :: MIDIEncoder(uint8_t pinA,uint8_t pinB,字节midiChannel,字节midiCCNumber)^ / var / folders / jy / f8dvlhcd4vdcvtl49bk8ytwc0000gn / T / builda847c0675e0bee2f5t5a。 8:89:注意:候选者是:在/var/folders/jy/f8dvlhcd4vdcvtl49bk8ytwc0000gn/T/builda847c0675e0bee2f5f05581e35ae65fe.tmp/sketch/MIDIEncoder.h:17:0的文件中,从/ var / folders / jy / f8dv0 builda847c0675e0bee2f5f05581e35ae65fe.tmp / sketch / MIDIEncoder.cpp:2:/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:72:2:注意:Encoder :: Encoder(uint8_t, uint8_t)编码器(uint8_t pin1,uint8_t pin2){^ /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/l ibraries/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()' ibraries / Encoder / Encoder.h:72:2:注意:候选人需要2个参数,提供了0个/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:69:7:注意:constexpr Encoder :: Encoder(const Encoder&)类Encoder ^ /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:69:7:注意:候选人需要1个参数,提供了0个/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Encoder/Encoder.h:69:7:注意:constexpr Encoder :: Encoder(Encoder &&)/Applications/Arduino.app/Contents/ Java /硬件/teensy/avr/libraries/Encoder/Encoder.h:69:7:注意:考生期望1个参数,0提供了不匹配的函数来调用'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. 从根本上讲,我相信编译器会告诉我找不到编码器库MIDIEncoder.cpp:8: error: no matching function for call to 'Encoder::Encoder()'的构造函数MIDIEncoder.cpp:8: error: no matching function for call to 'Encoder::Encoder()' ,但是我很困惑为什么。 Below is the full source for my library. 以下是我图书馆的完整资源。

MIDIEncoder.h 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 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. 特别要看数字3。

Encoder doesn't have a default constructor, and it is implicitly call here Encoder没有默认构造函数,在此处隐式调用

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. 您试图将其称为构造函数体,但是到那时“为时已晚” :)我的意思是,构造函数体执行时_enc必须已经初始化,但是_enc不能默认初始化,因此编译器抱怨。

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
{
    //...
}

You're initializing your _enc object too late. 您初始化_enc对象为时已晚。

In the beginning of the MIDIEncoder constructor body _enc should already be created, so the compiler tries to instantiate it using the (missing) default constructor and fails. 在MIDIEncoder构造体的开头_enc应该已经被创建,所以编译器将尝试使用(丢失)默认构造函数和失败,实例化。

Initialize it in the constructor initialization list instead: 而是在构造函数初始化列表中对其进行初始化:

MIDIEncoder::MIDIEncoder(uint8_t pin1, uint8_t pin2, ...) : _enc(pin1, pin2)
{

Actually, the same is true for other variables - there is no reason to initialize it inside constructor body. 实际上,其他变量也是如此-没有理由在构造函数体内对其进行初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM