Can't access the values of a std::key using a ComboBox :(


#1

Hi guys! So I have the following map. And I fill it as follows:

std::map<std::string, Array<int>> modeMap;
//...
modeMap.insert({"Major", scales.major});
modeMap.insert({"Natural Minor", scales.natMinor});

Each value is an Array so scales.major is defined earlier as {0,2,4,5,7,9,11}

Elsewhere in my code, the following works. I can get the Array of the major scale.

Array<int> scaleRef = theory.modeMap.operator[]("Major");

But if I try to program it to get the string value of dropdown menu, for example if the user chooses “Major” from the dropdown menu, it rejects it:

It doesn’t know what scaleName is I guess, but it will be a string and they will match the map…


#2

Don’t mix Juce Strings and std::strings in your Juce based code, you will just end up with issues like that. Prefer the Juce Strings and only convert to std::string or char* when some 3rd party library requires those.

So for example, make your map : std::map<String,Array<int>>

Hopefully, Juce String is compatible with std::map…


#3

Damn, you’re quick.

Yeah I switched the map to String instead of string and it works. Thanks again!


#4

also, it’s considered bad practice to call .operator[](std::string) explicitly like that.

see the examples here for proper usage: https://en.cppreference.com/w/cpp/container/map/operator_at


#5

Trying to read that was a little over my head sadly. Forgive me, I’m self-taught.

I needed something to behave like a dictionary in Javascript or Max/MSP. I needed to be able to provide a key and get it’s value back. .operator seems to do exactly that and is currently working for me perfectly. Is there a different function I should use to look up a key?


#6

Calling operator [ ] is just weird, when you could just as simply just use [ ], no other problem.

something = theorymap[scalename];

#8

Had no idea it was that simple, thanks!


#9

The language allows using the operator keyword and calling them like methods, but it’s useful only in esoteric situations.


#10

I see. I just assumed there had to be a function to do what I wantws so I looked until I came across operator


#11

Got a followup for you.

In situations where I’m accessing an OwnedArray called presets, I usually call it by saying presets[2]->etc. to access stuff in the second preset. However, when I create a pointer to the same owned array called processorPresets, I can’t access it the way you guys suggest.

When I do processorPresets[2]-> I get this error
Member reference type ‘OwnedArrayPianoRollComponent::Preset’ is not a pointer; did you mean to use ‘.’?
And switching it doesn’t fix it. But if I use my funny solution

processorPresets->operator[](2)->   

and so on, it works.

Do you know how I can avoid operator in this situation?


#12

Maybe something like :

(*processorPresets)[2]->

#13

Yep! Thanks again.