Cannot access public default constructor of juce::Slider

I’m attempting to create a std::vector of Slider objects for a GUI element, similar to the interface where you can draw harmonics in Ableton’s operator synth. Whenever I try to explicitly reference any default constructor, including Slider(), but also oddly including the ones I write myself, I’m told by the compiler that I cannot access that private member. Slider::Slider() is definitely listed under public functions in juce_Slider.h, so what am I missing, some embarrassingly simple C++ feature?

my function (resizeSliders is in a custom Component class auto-generated by the Projucer):


void ProbCurveEditorComponent::resizeSliders(int newNumSliders) {
    int old = sliders.size();
    sliders.resize(newNumSliders, Slider());  // <-- ERROR HERE
    for (old; old < sliders.size(); old++) {
        sliders[old].setSliderStyle(juce::Slider::LinearBarVertical);
        sliders[old].setRange(0, 1, 0.0001);
        sliders[old].setValue(0);

        //TODO: addlistener
        addAndMakeVisible(sliders[old]);
    }

    //update draw
    resized();
}



Command line output: 

1>D:\Program Files\VS19\VC\Tools\MSVC\14.24.28314\include\xmemory(671,1): error C2248: 'juce::Slider::Slider': cannot access private member declared in class 'juce::Slider'
1>D:\Program Files\JUCE\JUCE\modules\juce_gui_basics\widgets\juce_Slider.h(993): message : see declaration of 'juce::Slider::Slider'
1>D:\Program Files\JUCE\JUCE\modules\juce_gui_basics\widgets\juce_Slider.h(53): message : see declaration of 'juce::Slider'
1>D:\Program Files\VS19\VC\Tools\MSVC\14.24.28314\include\xmemory(1498): message : see reference to function template instantiation 'void std::_Default_allocator_traits<_Alloc>::construct<_Ty,const juce::Slider&>(_Alloc &,_Objty *const ,const juce::Slider &)' being compiled
1>        with
1>        [
1>            _Alloc=std::allocator,
1>            _Ty=juce::Slider,
1>            _Objty=juce::Slider
1>        ]
1>D:\Program Files\VS19\VC\Tools\MSVC\14.24.28314\include\xmemory(1498): message : see reference to function template instantiation 'void std::_Default_allocator_traits<_Alloc>::construct<_Ty,const juce::Slider&>(_Alloc &,_Objty *const ,const juce::Slider &)' being compiled
1>        with
1>        [
1>            _Alloc=std::allocator,
1>            _Ty=juce::Slider,
1>            _Objty=juce::Slider
1>        ]
1>D:\Program Files\VS19\VC\Tools\MSVC\14.24.28314\include\xmemory(1722): message : see reference to function template instantiation 'void std::_Uninitialized_backout_al<_Alloc>::_Emplace_back(const juce::Slider &)' being compiled
1>        with
1>        [
1>            _Alloc=std::allocator
1>        ]
1>D:\Program Files\VS19\VC\Tools\MSVC\14.24.28314\include\xmemory(1722): message : see reference to function template instantiation 'void std::_Uninitialized_backout_al<_Alloc>::_Emplace_back(const juce::Slider &)' being compiled
1>        with
1>        [
1>            _Alloc=std::allocator
1>        ]
1>D:\Program Files\VS19\VC\Tools\MSVC\14.24.28314\include\vector(1581): message : see reference to function template instantiation 'juce::Slider *std::_Uninitialized_fill_n<std::allocator>(juce::Slider *,unsigned __int64,const juce::Slider &,_Alloc &)' being compiled
1>        with
1>        [
1>            _Alloc=std::allocator
1>        ]
1>D:\Program Files\VS19\VC\Tools\MSVC\14.24.28314\include\vector(1579): message : while compiling class template member function 'juce::Slider *std::vector<juce::Slider,std::allocator>::_Ufill(juce::Slider *,const unsigned __int64,const _Ty &)'
1>        with
1>        [
1>            _Ty=juce::Slider
1>        ]
1>D:\Program Files\VS19\VC\Tools\MSVC\14.24.28314\include\vector(1233): message : see reference to function template instantiation 'juce::Slider *std::vector<juce::Slider,std::allocator>::_Ufill(juce::Slider *,const unsigned __int64,const _Ty &)' being compiled
1>        with
1>        [
1>            _Ty=juce::Slider
1>        ]
1>D:\Documents\CustomVST\probcurv\Source\ProbCurveEditorComponent.h(30): message : see reference to class template instantiation 'std::vector<juce::Slider,std::allocator>' being compiled
1>Done building project "probcurv_SharedCode.vcxproj" -- FAILED.

Components (including Slider) can not be copied, so you can’t really put them into containers like vectors directly, you need to use them via pointers. (The errors you are getting are not really about the default constructor but about the inability to copy construct or copy assign the Slider.)

1 Like

Got a chance to finally implement this, confirming for posterity’s sake that this was in fact the problem! Solution below is rather elegant:

image

That’s going to go horribly wrong…You are taking the address of a temporary variable. Didn’t you already get a crash or jassert at runtime?

Also, you shouldn’t really use raw pointers like with std::vector<Slider*>. Instead use Juce’s OwnedArray<Slider> or std::vector<std::unique_ptr<Slider>>

haha you’re absolutely right, haven’t built since I changed it, was just happy to get around the inaccessibility. This should just require memory allocation now

cool, I’ll look into the OwnedArray thing

Last update! OwnedArray is for sure the right structure/pattern, and I think I have a much better handle on the pointer logic now, thanks. Can now do this!

sliderresize

updated function, for posterity (sliders is now an OwnedArray ):
image

1 Like