maybe the question is a little trivial, but my c++ skills are pretty rough at the moment.
I’m trying to create a dynamic 2D array of TextButtons (or any other button) and fill it with TextButton instances and just can’t get it to work.
Which container type is recommended for this and what would the syntax look like? I already tried with std::vector and with juce::OwnedArray but I fail with both.
Looks like your problem is in the inner for loop. You create the TextButton and assign it to newTextButton, but you don’t actually add that button to your OwnedArray. Instead, you add a reference to a default-constructed temporary (which is a bad idea!):
I already did this in the creation loop and also tried your suggestion but it didn’t make a difference. It seems like in the creation loop the reference to the created TextButtons is lost. E.g. after 2 rows I see this in the debugger:
Again sorry for my lack of knowledge regarding modern c++, I had lived the last years in the somewhat friendly python world (and the not so friendly java world), but when it comes to real-time audio there seems to be no way around c++.
I finally made it work by switching from OwnedArrays to vector<unique_ptr>> like this:
stepCoreGridComponent::stepCoreGridComponent(int nColumns, int nRows)
: coreGridDimensions(nColumns, nRows)
for (int row = 0; row < coreGridDimensions.second; row++)
for (int column = 0; column < coreGridDimensions.first; column++)
auto newTextButton = std::make_unique<juce::TextButton>("test");
newTextButton->setBounds(column * 100, row * 100, 100, 100);
are there any strong reasons to use an OwnedArray and not use a vector?