No viable constructor

#1

I have a class called MidiMessageLogger that has a TextEditor property as follows…

class MidiMessageLogger
{
public:

	MidiMessageLogger()
	{		
	}
    // this fails telling me 'no viable constructor'
	MidiMessageLogger(TextEditor midiMessageBox) : messageBox(midiMessageBox)
	{
	}

	void logMessage(const MidiMessage& message, const String& timeCode);

private:
	TextEditor messageBox;
};

The constructor is not valid for some reason and VS tells me ‘no viable constructor’ with the red squiggles underneath where messagebox is set with midiMessageBox.

Why isn’t this allowed? I have other classes constructed in the same manner that have no issues being defined this way.

0 Likes

#2

This would call the copy constructor for messageBox, which is intentionally deleted with the macro JUCE_DECLARE_NON_COPYABLE_WITH__LEAK_DETECTOR, that is set for many of the JUCE classes.

Maybe you intended to have a reference as Messagebox, like:

private:
	TextEditor& messageBox;

…in which case you will have to remove the default constructor, because the reference will have to be initialised.

Hope that helps

1 Like

#3

Aha, well I don’t know what JUCE_DECLARE_NON_COPYABLE_WITH__LEAK_DETECTOR, actually means or what it’s purpose is but at least I know what to look for when I encounter this again! Daniel - you are my C++ hero! :slight_smile: Thank you again.

I will try to do some research on this ‘non-copyable’ term.

0 Likes

#4

It makes the copy constructor private (i.e. inaccessible), because e.g. Components must not be copied.
And additionally it adds a JUCE leak detector to the class. When you close your app/plugin and forgot to delete - implicitly by owning the class via unique_ptr or OwnedArray or explicitly using delete (tut tut, as a nice assert puts it :wink: ) - you will hit an assert, that you are leaking an instance of that class.

It is worthwhile to add that to all your classes, that are not trivially copyable. Saves some headaches later on

2 Likes