我正在尝试创建一个类filePlayerGui的数组,当我尝试在构造函数中对其进行初始化时,它不会让我这样做

class MainComponent   : public Component,
                    public MenuBarModel
{
public:
//==============================================================================
/** Constructor */
MainComponent (Audio& audio_);

/** Destructor */
~MainComponent();

void resized() override;

//MenuBarEnums/Callbacks========================================================
enum Menus
{
    FileMenu=0,

    NumMenus
};

enum FileMenuItems
{
    AudioPrefs = 1,

    NumFileItems
};
StringArray getMenuBarNames() override;
PopupMenu getMenuForIndex (int topLevelMenuIndex, const String& menuName) override;
void menuItemSelected (int menuItemID, int topLevelMenuIndex) override;

private:
Audio& audio;
FilePlayerGui filePlayerGui[2] {audio.getFilePlayer(0), audio.getFilePlayer(1)};



//==============================================================================
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

filePlayerGui出现此错误“复制类型为'FilePlayerGui'的数组元素会调用已删除的构造函数”。我尝试在下面的.cpp文件中进行初始化,但随后它告诉我需要数组初始化程序
MainComponent::MainComponent (Audio& audio_) : audio (audio_)
{


setSize (500, 400);
addAndMakeVisible(filePlayerGui[0]);
addAndMakeVisible(filePlayerGui[1]);
}

MainComponent::~MainComponent()
{

}

 void MainComponent::resized()
 {
filePlayerGui[0].setBounds (0, 0, getWidth(), 40);
 }

   //MenuBarCallbacks==============================================================
 StringArray MainComponent::getMenuBarNames()
{
const char* const names[] = { "File", 0 };
return StringArray (names);
}

PopupMenu MainComponent::getMenuForIndex (int topLevelMenuIndex, const String& menuName)
{
PopupMenu menu;
if (topLevelMenuIndex == 0)
    menu.addItem(AudioPrefs, "Audio Prefrences", true, false);
return menu;
}

void MainComponent::menuItemSelected (int menuItemID, int topLevelMenuIndex)
{
if (topLevelMenuIndex == FileMenu)
{
    if (menuItemID == AudioPrefs)
    {
        AudioDeviceSelectorComponent audioSettingsComp (audio.getAudioDeviceManager(),
                                                        0, 2, 2, 2, true, true, true, false);
        audioSettingsComp.setSize (450, 350);
        DialogWindow::showModalDialog ("Audio Settings",
                                       &audioSettingsComp, this, Colours::lightgrey, true);
    }
}
}

最佳答案



表示此行的某些变体在FilePlayerGui的声明中:

FilePlayerGui(const FilePlayerGui&) = delete

表示无法复制FilePlayerGui:https://en.cppreference.com/w/cpp/language/copy_constructor#Deleted_implicitly-declared_copy_constructor

有解决方法,但您正在避开FilePlayerGui的意图。因此正确的答案应在此处停止。 FilePlayerGui的任何使用都应通过audio类的成员完成,使用audio.getFilePlayer(0)audio.getFilePlayer(1)。 (如果这是MainComponent的唯一成员,则应该消除MainComponent的所有可能性。)

如果您想知道如何做坏事,我会在任何代码审查中都拒绝这样做,则可以将filePointerGui创建为:
  • FilePointerGui* filePointerGui[2]将被初始化为:

  • MainComponent::MainComponent(Audio& audio_) : audio(audio_), filePointerGui({ &audio.getFilePlayer(0), &audio.getFilePlayer(1) })
    

    访问将按以下方式执行:
    *filePointerGui[0]
    


  • reference_wrapper<FilePointerGui> filePointerGui[2],它将初始化为:

  • MainComponent::MainComponent(Audio& audio_) : audio(audio_), filePointerGui({ audio.getFilePlayer(0), audio.getFilePlayer(1) })
    

    访问将按以下方式执行:
    filePointerGui[0].get()
    



    可能值得再次强调一下,这是不好的代码,因为它试图超越语言并围绕类设计的意图进行工作。但这是c++,因此您可以做到。

    09-05 06:50