Creating an instance of Progress Bar : "no default constructor available"


#1

Hello, this is a very beginner level question but I am pretty lost here… I am looking to just create an instance of the Progress Bar, have it appear in my component and react to a variable.

The part I am lost on is that my compiler gives the error:

" juce::ProgressBar’: no appropriate default constructor available "

Can anyone just give me a basic rundown of how I would create one of these instances, and include what I have to pass into this object? I am assuming its a reference to a variable (double as its listed in the docs) but I am lost as to how I would actually write this in my editor… thanks!


#2

You need to add a double type parameter in your editor class and pass that into the ProgressBar constructor. Note that the double member should be before the ProgressBar member in the class members to avoid a compiler warning and possible runtime errors.

class MyComponent : public Component
{
public:
  MyComponent() : m_progressbar(m_progress)
  {
    // your constructor body with addAndMakeVisible(m_progressbar); etc
  }
private:
  double m_progress = 0.0;
  ProgressBar m_progressbar;
};

#3

You can initialize member variables without default constructors like this, without using the member initializer list:

private:
    double m;
    T t{m}; //assumes T(double) is the only ctor

#4

:warning: if you don’t provide a default constructor, m is very likely to be uninitialized.

So I guess it should be:

private:
    double m = 42.0;
    T t{m};

#5

Whoops, forgot that bit! No uninitialized variables!