gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:01:05 25 4
gpt4 key购买 nike

我正在创建一个用于 Sketch 的 Arduino 库。它使用 Encoder Library , 但在编译时我得到一个迟钝的错误:

/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()',但我不知道为什么。以下是我的图书馆的完整来源。

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;
}
}

下面有一些很好的答案。现在我知道要寻找什么了,我找到了 this resource这解释了我在哪里犯了错误。特别要看数字 3。

最佳答案

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

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

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

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

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

关于c++ - Arduino 库中没有调用的匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38905366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com