Can't seem to add a unique_ptr a Component with constructor parameter

Hi , I’ll try and be brief. I have a compnent that has this as a constructor:-

	Panel(MyplugAudioProcessor &d)

In PluginEditor.h I have:

 std::unique_ptr<Panel> pPanel;

in the editor I make it with:

pPanel = std::make_unique<Panel>(p);

Which works fine until I have to add it with


Then I get an error:

Error C2664 ‘void juce::Component::addAndMakeVisible(juce::Component &,int)’: cannot convert argument 1 from ‘std::unique_ptr<Panel,std::default_delete<_Ty>>’ to ‘juce::Component *’

Does anyone know how I can get around this. I was doing so well for an std: noob! :slight_smile:

Sure, just get the raw pointer from the unique_ptr using:

addAndMakeVisible (pPanel.get());

For safety reasons the std:: variants often need explicit methods, another example is std::atomic::load() instead of using the value directly…


1 Like

Thanks man that’s got it, I didn’t know about that at all.

I want ScopedPointer back!! :slight_smile:

1 Like

It never went anywhere. you can keep using it.

1 Like

They don’t replace ALL occurrences of it in the library for no reason.

Feel free to read the “Supporting OS X 10.6” thread where they discuss it being removed but not being deprecated.

addAndMakeVisible also takes a reference so it’s preferable to write

addAndMakeVisible (*pPanel);

(if you know it’s not a nullptr)

Thanks Jules. I thought that would copy the whole ‘panel’ class to the function, which would be a waste. I have already read the thread about 10.6 and that you’re keeping ScopedPointer to prevent existing code from breaking. But it feels like, a ‘for now’ thing.

No, it takes a reference.


That’s the whole point of using smart pointers. They make it hard to copy stuff, so it doesn’t happen by accident.
Smart pointers create little objects on the stack, so that normal life time methods apply (i.e. getting deleted when the scope ends), even though the actual object lives on the bigger heap memory.

1 Like