Error C2662 using MidiMessage::setNoteNumber(int)

Hi,

I’m trying to make my firt midi app work, compiling on Visual Studio. I managed to set up a midi input device, a midi output device, and it works perfectly. My problem is that when I call the function .setNoteNumber(), Visual Studio throws an error :

error C2662: ‘void juce::MidiMessage::setNoteNumber(int) noexcept’ : cannot convert ‘this’ pointer from ‘const juce::MidiMessage’ to ‘juce::MidiMessage &’

Here is my code :

void MainComponent::handleIncomingMidiMessage(const MidiMessage& message)
{
    if(message.isNoteOnOrOff())
    {
        int n = message.getNoteNumber();
        int a = myFunction(n);
        message.setNoteNumber(n+a); // this line...
    }
    auto output = deviceManager.getDefaultMidiOutput();
    output->sendMessageNow(message);
} 

Any idea what I should try ?

Thanks in advance !

The argument into the function is a const reference, so you can not call non-const methods like setNoteNumber on the object. You need to create a copy of the MIDI message object, change the note number on that and then send that copied message to the output.

You might also want to check if it’s sensible to get the default MIDI output device each time the message handler is called. There may be expensive initialization involved. It would be better to have the MIDI output device pointer as a member variable and initialize that in your constructor.

2 Likes

Thanks a lot !