Midi keyboard component constructor

Trying to create a midi keyboard component when I create an instance of one in my editor.h I attempt to use this instance and get a message no default constructor where in my editor.cpp am I to put the constructor for my midi keyboard component

The problem is not where to put it, but the MidiKeyboardComponent needs a MidiKeyboardState and the orientation as arguments.
The MidiKeyboardState is separate from the MidiKeyboardComponent, because you want to have the state in the processor so it can process the midi events regardless if the editor is shown or not.

Your processor needs to give access th the MidiKeyboardState and you use that reference in the MidiKeyboardComponent constructor.

I think this is also a bit of a c++ question
do i need to declare the constructor for the MidiKeyboardComponent in the AudioProcessor class or outside of it .
when it is declared can i use it as an instance like a slider I thought i could use it like adding a slider but that did not work

Please have a look at this tutorial. There is example code that could help you to understand.

Midi input tutorial

I have a similar problem: no default constructor exists for midikeyboardcomponent.
If I add this code in the editor constructor:

mMidiKeyBoard = juce::MidiKeyboardComponent(mkeyboardState, juce::MidiKeyboardComponent::Orientation::horizontalKeyboard);

then I get the message that the constructor is inaccessible??
How can I add a MidiKeyboardComponent to my VST plugin??

This code calls the copy assignment constructor which doesn’t exist for components.

Either you supply the arguments directly at construction or you need to use heap allocation with a unique_ptr.

(post deleted by author)

OK - found out that I need to initialize the midikeyboard in the member initialization list of the AudioProcessorEditor constructor, so something like this:

ProgressorAudioProcessorEditor::ProgressorAudioProcessorEditor (ProgressorAudioProcessor& p)
: mMidiKeyBoard (mkeyboardState, juce::MidiKeyboardComponent::Orientation::horizontalKeyboard),
AudioProcessorEditor (&p), audioProcessor (p)


Yes, that is the way to do it. Sorry I was on tablet this morning and writing code there is painful.

What I usually do, since the keyboardState needs to be in the processor in order to process the midi messages independently from the presence of an editor, I expose the keyboardState either as a public variable in the processor or as reference (preferred):

MidiKeyboardState& ProgressorAudioProcessor::getKeyboardState()
    return mKeyboardState;

// and in your constructor:
ProgressorAudioProcessorEditor::ProgressorAudioProcessorEditor (ProgressorAudioProcessor& p)
  : AudioProcessorEditor (&p),
    mMidiKeyBoard (p.getKeyboardState(), juce::MidiKeyboardComponent::Orientation::horizontalKeyboard),
    audioProcessor (p)
    // etc

A note about your code: be pedantic about the order when you declare your members and the order of the initialisation!
First call the base classes constructor, then all the members in order of appearance in the class declaration.
If you access them in the wrong order you might call uninitialised memory and crash.