Help with Error

Hello, I wonder could anyone help, I was following the Kadnze tutorials to build a plugin, but got this error:

Constructor for ‘KadenzeAudioProcessor’ must explicitly initialize the member ‘mGainParameter’ which does not have a default constructor

No viable overloaded ‘=’

#ifndef JucePlugin_PreferredChannelConfigurations
: AudioProcessor (BusesProperties()
#if ! JucePlugin_IsMidiEffect
#if ! JucePlugin_IsSynth
.withInput (“Input”, juce::AudioChannelSet::stereo(), true)
.withOutput (“Output”, juce::AudioChannelSet::stereo(), true)
addParameter(mGainParameter = new juce::AudioParameterFloat(“gain”,“Gain”,0.0f,1.0f,0.5f));


Is this the right way to paste code in this forum?


addParameter(mGainParameter = new juce::AudioParameterFloat(“gain”,“Gain”,0.0f,1.0f,0.5f));

this line looks sketchy. you assign a new AudioParameterFloat into mGainParameter, but simultanously feed the whole statement into a method called addParameter?
could it be that you meant to write:

mGainParameter = new juce::AudioParameterFloat(“gain”,“Gain”,0.0f,1.0f,0.5f);

in which case i suppose mGainParameter is either

juce::AudioParameterFloat* // or

it should be the 2nd one. then you could rewrite that one line from

mGainParameter = new juce::AudioParameterFloat(“gain”,“Gain”,0.0f,1.0f,0.5f);
// to
mGainParameter = std::make_unique<juce::AudioParameterFloat>(“gain”,“Gain”,0.0f,1.0f,0.5f);

to give away the responsibility of the garbage collection to this smart pointer, called unique_ptr. it’s basically just a wrapper around a raw pointer that makes sure that the thing gets automatically destroyed when the scope ends. it also restricts the pointer from being used anywhere else, but that is not important here.

one last thing to note: you might be better off implementing a juce::AudioProcessorValueTreeState rather than making all the parameters individually. it is an object that handles a lot of stuff related to parameters and serialization (which means saving and loading stuff)

Thanks a mill, for the help, I will try this.

BTW how did you get the code to look like this?

edit: ah shit, what a typo xD likeThis* anyway

That actually is a correct way to do it. AudioProcessor::addParameter’s documentation says :

An equivalent way of doing it would be :

mGainParameter = new juce::AudioParameterFloat(“gain”,“Gain”,0.0f,1.0f,0.5f);

Using a smart pointer like std::unique_ptr would be a mistake here because it would lead to a double deletion of the object. The mGainParameter should be left as a raw pointer of type AudioParameterFloat*.

Smart pointers should be used whenever possible, but there are exceptions to that rule. For example Juce has several APIs like AudioProcessor::addParameter that need a raw pointer to be passed in and the ownership of the object will be given to the receiving object. Whether an ownership transfer happens or not with a method needs to be confirmed from the documentation of the class/method.

1 Like

i see. looks like i just wasn’t familiar with addParameter and its ability to automatically collect garbage. but the biggest confusion came from the fact that mGainParameter was set in the same line than feeding it forward to addParameter. that made it look syntactically weird. made me ask myself what exactly is the thing an equal-operator returns, so that it can be given to a method, you know? anyway, using apvts (audioProcessorValueTreeState) solves a lot of this, because you don’t even have to call addParameter manually anymore