Initialize array of ShapeButton

Hi everyone,

I have a silly question, that I just spent a while on to no avail.

Say I have this in pluginEditor.h:

ShapeButton array[5];

How do I properly initialize it in pluginEditor.cpp?

I keep getting “call to deleted constructor”. I’ve tried using a vector, initializing the array directly in the header file, and initializing array in the initializer of pluginEditor.cpp, but I keep getting the same problem.

There might be some sneaker/cleaner way to do that, but the following does work :
In your editor header declarations :

std::vector<std::unique_ptr<juce::ShapeButton>> shapebuttons;

And in your editor constructor body :

shapebuttons.push_back(std::make_unique<juce::ShapeButton>("but1",juce::Colours::red,juce::Colours::blue,juce::Colours::green));
shapebuttons.push_back(std::make_unique<juce::ShapeButton>("but2",juce::Colours::red,juce::Colours::blue,juce::Colours::green));
shapebuttons.push_back(std::make_unique<juce::ShapeButton>("but3",juce::Colours::red,juce::Colours::blue,juce::Colours::green));
shapebuttons.push_back(std::make_unique<juce::ShapeButton>("but4",juce::Colours::red,juce::Colours::blue,juce::Colours::green));
shapebuttons.push_back(std::make_unique<juce::ShapeButton>("but5",juce::Colours::red,juce::Colours::blue,juce::Colours::green));
for (auto& but : shapebuttons)
{
   addAndMakeVisible(*but);
}

Thanks I really appreciate it! Do you know why add make_unique makes this work?

For example, doing something like

std::vector<juce::ShapeButton> shapebuttons;

and

shapebuttons.push_back(juce::ShapeButton("but1",juce::Colours::red,juce::Colours::blue,juce::Colours::green));

does not work. Why is that?

std::vector<juce::ShapeButton> shapebuttons;

Will require the vector elements to be copyable/moveable values, but the Juce components have that explicitly disabled. (It could make quite a mess if Components could be copied around as values.)

Using pointers gets around that issue.

1 Like

I found a related post that was helpful for anyone else interested in learning more.