2-Dimensional Owned Arrays


#1

Hi all, I’m pretty new to Juce and C++ in general so this may seem like a simple question (my apologies).

I am creating a 2D array of TextButton’s which will later be used to start different audio samples. This array is automatically created from a folder I have containing the samples which are named accordingly eg. 0_0, 0_1, 1_0, 1_2, 1_3 etc.

I have created the array as follows:

As far as I can tell I am creating the elements in the array but am having problems accessing them afterwards. filePlayers[] is another array that I am using to keep a player for each audio file. It carries the name of the file it relates to which is what I am using to create the buttons.

It may seem a bit cryptic but the first number (firstNum) relates to the artist and the second one (secondNum) the track. Therefore if the track is 0 a new array element should be created to hold a new set of buttons.

int firstNum  = ( (filePlayers[i]->fileName.substring(0, 1)).getIntValue());
int secondNum = (filePlayers[i]->fileName.substring(2, 3).getIntValue());
			
// create a button for it
if ( secondNum == 0 )
	playerButtons.add( new OwnedArray<TextButton> );

if ( playerButtons[firstNum] != 0 )
	playerButtons[firstNum]->add(new TextButton( filePlayers[i]->fileName).substring(0, 3), (String("playerButton")<<filePlayers[i]->fileName.substring(0, 3) ));

Now when I try to addAndMakeVisible the TextButton stored in each position
with: addAndMakeVisible(playerButtons2[firstNum][secondNum]); I get the following error: error: no matching function for call to ‘MainComponent::addAndMakeVisible(juce::OwnedArray<juce::TextButton, juce::DummyCriticalSection>&)’

Surely using the [] operator on the pointer returned playerButtons[] which should be a pointer to the TextButton array should access the pointer for that TextButton object and so be able to pass to the addAndMakeVisible method? Am I doing something majorly wrong here with the [][] notation?

Like I said I am new to the programming world so any comments would be helpful. By the way I am having great fun building apps with Juce, I am finding it (mostly) straightforward to learn and the docs and forums are a great help. Keep up the good work Jules!


#2

Doing playerButtons2[][], the first [] operates on the outer OwnedArray returns a pointer to one of the inner OwnedArrays. The second [] therefore operates on the pointer to that OwnedArray, it needs to operate on the OwnedArray not a pointer to it.

You could do:

or something like:

Neither of which has the elegance you seem to be after with this approach!

Perhaps:

Is less ugly if you’re sure firstNum and secondNum are valid.


#3

Cheers, spot on.