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/builda847c0675e0bee2f5f05f05581e35ae65fe.tmp/sketch/MIDIEncoder.cpp:在构造函数MIDIEncoder :: MIDIEncoder(uint8_t,uint8_t:字节,字节,MIDIEncoder.cpp:8:错误:调用"Encoder :: Encoder()"没有匹配函数MIDIEncoder :: MIDIEncoder(uint8_t pinA,uint8_t pinB,字节midiChannel,字节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()' /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()'从根本上讲,我相信编译器会告诉我找不到编码器库的构造器 MIDIEncoder.cpp:8:错误:没有匹配的函数来调用'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 valuesconst byte decrementValue = 2; // A constant for the start of decrement valuesMIDIEncoder::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; }}以下是一些很好的答案.现在,我知道要查找的内容了,我找到了此资源解释了我犯错的地方.特别要看数字3.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 hereMIDIEncoder::MIDIEncoder(uint8_t pin1, uint8_t pin2, byte midiChannel, byte midiCCNumber) /*Implicit constructor call*/ { //...}您尝试将其称为构造函数体,但是到那时为时已晚" :)我的意思是,构造函数体执行时,必须已经初始化 _enc ,但是 _enc 进行默认初始化,因此编译器会抱怨.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-28 02:46