Problems w/ Graphics > The Application Window Tutorial (ScopedPointer deprecated)

OK, very simple tutorial (code below). When attempting to build/run on Mac OSX Mojave, I first receive the warnings that “~ScopedPointer is deprecated” - but the tutorial runs.

Following the warnings around, I read in the JUCE source that:

  // ScopedPointer is deprecated! You should use std::unique_ptr instead.
    JUCE_DEPRECATED_ATTRIBUTE inline ScopedPointer() = default;

So I change the variable in the tutorial to:

   //ScopedPointer<MainWindow> mainWindow;
   std::unique_ptr<MainWindow> mainWindow;

However, now when I try to build it, I receive the error “no viable overloaded ‘=’” on this line:

mainWindow = new MainWindow (getApplicationName());

What would be the correct way nowadays for this?

//==============================================================================
class MainWindowTutorialApplication  : public JUCEApplication
{
public:
    //==============================================================================
    MainWindowTutorialApplication() {}

    const String getApplicationName() override       { return ProjectInfo::projectName; }
    const String getApplicationVersion() override    { return ProjectInfo::versionString; }
    bool moreThanOneInstanceAllowed() override       { return true; }

    //==============================================================================
    void initialise (const String& commandLine) override
    {
        // Add your application's initialisation code here..
        mainWindow = new MainWindow (getApplicationName());
        
    }

    void shutdown() override
    {
        // Add your application's shutdown code here..
        mainWindow = nullptr;
        
    }

    //==============================================================================
    void systemRequestedQuit() override
    {
        // This is called when the app is being asked to quit: you can ignore this
        // request and let the app carry on running, or call quit() to allow the app to close.
        quit();
    }

    void anotherInstanceStarted (const String& commandLine) override
    {
        // When another instance of the app is launched while this one is running,
        // this method is invoked, and the commandLine parameter tells you what
        // the other instance's command-line arguments were.
    }
     
    //==============================================================================
    class MainWindow    : public DocumentWindow
    {
    public:
        MainWindow (String name)  : DocumentWindow (name,
                                                    Colours::lightgrey,
                                                    DocumentWindow::allButtons)
        {
            centreWithSize (300, 200);
            setVisible (true);
        }
 
        void closeButtonPressed() override
        {
            JUCEApplication::getInstance()->systemRequestedQuit();
        }
 
    private:
        JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainWindow)
    };
 
private:
    std::unique_ptr<MainWindow> mainWindow;
    //ScopedPointer<MainWindow> mainWindow;
};

//==============================================================================
// This macro generates the main() routine that launches the app.
START_JUCE_APPLICATION (MainWindowTutorialApplication)

For example :

mainWindow.reset(new MainWindow (getApplicationName()));

thank you!

Another option:
mainWindow = std::make_unique<MainWindow>(getApplicationName());

(the Projucer generates the code @Xenakios gave for a GUI Application and not this, is there any reason one is better than the other?)

Possibly it’s for broader compatibility: make_unique was only introduced in C++14, whereas the syntax with reset() and new is valid since C++11

1 Like

When using std::make_unique, you won’t necessarily get IDE hints for the constructor arguments. (At least VS2017 and Xcode 9 don’t manage to do that.)

2 Likes

I suggest updating the JUCE tutorial page with this information in the section that states what to add to the initialise() function. (I was stuck on getting that “no viable overloaded ‘=’” error while working through the tutorial.)

https://docs.juce.com/master/tutorial_main_window.html