如何在数组中初始化此 class?
How can I initialise this class in an array?
我正在尝试创建一个 class 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 出现此错误 "Copying array element of type 'FilePlayerGui' invokes deleted constructor"。我已经尝试在下面的 .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);
}
}
}
Copying array element of type FilePlayerGui
invokes deleted constructor
意味着这一行的一些变体在 FilePlayerGui
:
的声明中
FilePlayerGui(const FilePlayerGui&) = delete
表示无法复制FilePlayerGui
:https://en.cppreference.com/w/cpp/language/copy_constructor#Deleted_implicitly-declared_copy_constructor
有变通办法,但您绕过了 FilePlayerGui
作者对 class 的意图。所以正确答案应该到此为止。 FilePlayerGui
的任何使用都应通过 classes 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()
可能值得再次强调这是糟糕的代码,因为它试图超越语言并绕过 classes 设计的意图。但这是 c++ 所以你 可以 做到。
我正在尝试创建一个 class 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 出现此错误 "Copying array element of type 'FilePlayerGui' invokes deleted constructor"。我已经尝试在下面的 .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);
}
}
}
Copying array element of type
FilePlayerGui
invokes deleted constructor
意味着这一行的一些变体在 FilePlayerGui
:
FilePlayerGui(const FilePlayerGui&) = delete
表示无法复制FilePlayerGui
:https://en.cppreference.com/w/cpp/language/copy_constructor#Deleted_implicitly-declared_copy_constructor
有变通办法,但您绕过了 FilePlayerGui
作者对 class 的意图。所以正确答案应该到此为止。 FilePlayerGui
的任何使用都应通过 classes 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()
可能值得再次强调这是糟糕的代码,因为它试图超越语言并绕过 classes 设计的意图。但这是 c++ 所以你 可以 做到。