I need to pass variables to a lamda expression inside a method. To do this I'm using the &
operator in the capture list.
(I'm using the AppleMIDI library.)
// MIDIService.h
#ifndef MIDIService_h
#define MIDIService_h
#include <AppleMIDI.h>
// ...
class MIDIService {
public:
MIDIService();
void setup(const uint8_t midiOnValue,
// ...
void (*onConnected)());
// ...
};
#endif
// MIDIService.cpp
#include "MIDIService.h"
MIDIService::MIDIService() {}
void MIDIService::setup(const uint8_t midiOnValue,
// ...
void (*onConnected)()) {
// ...
// AppleMIDI.setHandleConnected is from the AppleMIDI library.
AppleMIDI.setHandleConnected([&](const APPLEMIDI_NAMESPACE::ssrc_t& ssrc, const char* name) { // ERROR!
onConnected();
});
// ...
// MIDI.setHandleControlChange is from the AppleMIDI library.
MIDI.setHandleControlChange([&](byte channel, byte number, byte value) { // ERROR!
if (value == midiOnValue) {
// ...
} else {
// ...
}
});
}
But VSCode spits out:
no suitable conversion function from "lambda [](const appleMidi::ssrc_t &ssrc, const char *name)->void" to "void (*)(const appleMidi::ssrc_t &, const char *)" exists
and
no suitable conversion function from "lambda [](byte channel, byte number, byte value)->void" to "midi::ControlChangeCallback" exists
I researched about this error but couldn't find a solution that worked for me. Being a C++ newbie I also had trouble understanding half of them.