Make unique AudioProcessorValueTreeState and passing state unique_ptr reference


I am trying to follow an outdated tutorial and convert it to more C++ as it’s using old JUCE specific pointer type.

I got a drive knob and drive attachment for the GUI. Which is as below:

std::unique_ptr<juce::Slider> driveKnob;
std::unique_ptr<juce::AudioProcessorValueTreeState::SliderAttachment> driveAttachment;

I successfully implemented driveKnob like so:

driveKnob = std::make_unique<juce::Slider>("Drive");
driveKnob->setTextBoxStyle(juce::Slider::TextEntryBoxPosition::NoTextBox, false, 100, 100);

Then I try to init driveAttachment like so:
driveAttachment = std::make_unique<juce::AudioProcessorValueTreeState::SliderAttachment>(p.getState(), "drive", driveKnob.get());

the state related definitions from the processor are below:

    juce::AudioProcessorValueTreeState& getState();
    std::unique_ptr<juce::AudioProcessorValueTreeState> state;

And getState function is below:

juce::AudioProcessorValueTreeState& DistortionAudioProcessor::getState()
    return *state;

The error I am getting is

|Severity|Code|Description|Project|File|Line|Suppression State|
|Error|C2664|'juce::AudioProcessorValueTreeState::SliderAttachment::SliderAttachment(juce::AudioProcessorValueTreeState &,const juce::String &,juce::Slider &)': cannot convert argument 3 from '_Ty' to 'juce::Slider &'|Distortion_SharedCode|C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include\memory|3580||

And for more visual people here is the screenshot of the error log:

the function template looks like so:

// FUNCTION TEMPLATE make_unique
template <class _Ty, class... _Types, enable_if_t<!is_array_v<_Ty>, int> = 0>
_NODISCARD unique_ptr<_Ty> make_unique(_Types&&... _Args) { // make a unique_ptr
    return unique_ptr<_Ty>(new _Ty(_STD forward<_Types>(_Args)...));

As the error says, the third argument you’ve given to SliderAttachment isn’t of the type it was expecting. SliderAttachment expects a juce::Slider& but you gave it a juce::Slider*.

Replace driveKnob.get() with *driveKnob when creating the attachment.

FWIW, you don’t need to use unique_ptr for sliders. You can declare it on the stack instead and call setName on the component instead of passing the name to the constructor.

1 Like

Thank you I will have a look this evening! However, when I try to pass *driveKnob I get an exception within xtree. Screenshot here:

How are you initialising the tree? Do you definitely have a parameter with the id “drive”?