本文介绍了Arduino 库中没有匹配的调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用于 Sketch 的 Arduino 库.它使用 Encoder Library,但是在编译时出现了一个钝错误:>

/var/folders/jy/f8dvlhcd4vdcvtl49bk8ytwc0000gn/T/builda847c0675e0bee2f5f05581e35ae65fe.tmp/sketch/MIDIEncoder.cpp:在构造函数中:: 'MIDIEncoderuint, 'MIDIEncoderuint', byteEncoderuintMIDIEncoder.cpp:8: 错误:没有匹配的函数调用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:注意:候选人是:在/var/folders/jy/f8dvlhcd4vdcvtl49bk8ytwc0000gn/T/builda847c0675e0bee2f5f05581e35ae65fe.tmp/sketch/MIDIEncoder.h:17:0 包含的文件中,来自/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: 注意:Encoder::Encoder(uint8_t, uint8_t)编码器(uint8_t pin1,uint8_t pin2){^/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/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&)类编码器^/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/hardware/teensy/avr/libraries/Encoder/Encoder.h:69:7:注意:候选人需要 1 个参数,0 提供没有匹配的函数调用 'Encoder::Encoder()'

从根本上说,我相信编译器告诉我它找不到 Encoder 库的构造函数 MIDIEncoder.cpp:8: error: no matching function for call to 'Encoder::Encoder()',但我不知道为什么.以下是我的图书馆的完整来源.

MIDIEncoder.h

/*MIDI编码器.h用于从旋转编码器创建相关 MIDI CC 消息的库.由 Paul Williamson 创建,2016 年 8 月 11 日.项目来源可在:http://github.com/squarefrog/teensy-midi-encoder-box发布到公共领域.*/#ifndef MIDIEncoder_h#define MIDIEncoder_h#include "Arduino.h"#include <Encoder.h>MIDI 编码器类{民众:MIDIEncoder(uint8_t pin1, uint8_t pin2, byte midiChannel, byte midiCCNumber);字节通道;字节 ccNumber;字节读取();私人的:无符号长 _lastTurnedTime;长_oldPosition;编码器_enc;};#万一

MIDIEncoder.cpp

#include "MIDIEncoder.h"#include <Encoder.h>常量字节增量值 = 66;//增量值开始的常量const 字节减量值 = 2;//递减值开始的常量MIDIEncoder::MIDIEncoder(uint8_t pin1, uint8_t pin2, byte midiChannel, byte midiCCNumber){频道 = midiChannel;ccNumber = midiCCNumber;_enc = 编码器(pin1, pin2);_oldPosition = -999;_lastTurnedTime = 毫秒();}字节 MIDIEncoder::read(){long newPosition = _enc.read();//如果位置没有改变,忽略.如果(新位置 == _oldPosition){返回0;}//如果位置不能被 4 整除,则忽略.如果(新位置%4!= 0){返回0;}unsigned long delta = millis() - _lastTurnedTime;字节偏移= 0;//应用粗加速如果 (delta  99 && delta  179 && delta  _旧位置){返回增量值 + 偏移量;} 别的 {返回减量值 + 偏移量;}}

下面有一些很好的答案.现在我知道要找什么了,我找到了 这个资源 这解释了我在哪里犯了错误.特别注意数字 3.

解决方案

Encoder 没有默认构造函数,这里隐式调用

MIDIEncoder::MIDIEncoder(uint8_t pin1, uint8_t pin2, byte midiChannel, byte midiCCNumber)/*隐式构造函数调用*/{//...}

您尝试将其称为构造函数体,但到那时为时已晚":) 我的意思是,_enc 必须在构造函数体执行时已经初始化,但是 _enc 不能默认初始化,所以编译器报错.

这就是构造函数初始化列表的用途:

MIDIEncoder::MIDIEncoder(uint8_t pin1, uint8_t pin2, byte midiChannel, byte midiCCNumber): _enc(pin1, pin2)//为_enc调用合适的构造函数,不是默认的{//...}

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

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.

解决方案

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

这篇关于Arduino 库中没有匹配的调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:51