Search code examples
c++user-interfacewxwidgets

Why is Show() inaccessible?


In wxwidget to create frame you create an instance of it on the heap. So in accessing it member function you are to use the -> operator. I have created a wxMDIParentFrame and i am trying to create an instance of a child frame when one of the menus of my Parent frames are clicked. But after i create the instance of my child class it gives me an error trying to call the show function for my childframe

Here's the code

MyFrame.h

#pragma once
#include <wx/wx.h>
 
class MyFrame:public wxMDIParentFrame
{
public :
    MyFrame(const wxString& title);
    ~MyFrame();

private:
    wxStatusBar* stb = nullptr;
    wxMenuBar* menubar = nullptr;
    wxToolBar* toolbar = nullptr;

    

    void OnExitMenu(wxCommandEvent& evt);
    void OnNewMenu(wxCommandEvent& evt);
    void OnSelectColor(wxCommandEvent& evt);
    wxDECLARE_EVENT_TABLE();
};


MyFrame.cpp

#include "MyFrame.h"
#include <wx/wx.h>
#include "IDs.h"
#include "childFrame.h"


wxBEGIN_EVENT_TABLE(MyFrame,wxFrame)
    EVT_MENU(exitID,MyFrame::OnExitMenu)
    EVT_MENU(newID,MyFrame::OnNewMenu)
wxEND_EVENT_TABLE()
MyFrame::MyFrame(const wxString& title) :wxMDIParentFrame(nullptr, wxID_ANY, title, wxPoint(30, 30), wxSize(800, 800))
{

    menubar = new wxMenuBar();
    this->SetMenuBar(menubar);
    

    wxMenu* menuFile = new wxMenu();
    menubar->Append(menuFile, "FILE");
    menuFile->Append(newID, "New");
    menuFile->Append(openID, "Open");
    menuFile->Append(saveID, "Save");
    menuFile->Append(exitID, "Exit");
    

    toolbar =this->CreateToolBar(wxTB_HORIZONTAL, wxID_ANY);
    

    wxColor colours[16];
    colours[0] = wxColor(0, 0, 0);
    colours[1] = wxColor(0, 0, 128);
    colours[2] = wxColor(0,128,0);
    colours[3] = wxColor(0, 128, 128);
    colours[4] = wxColor(128, 0, 0);
    colours[5] = wxColor(128, 0, 128);
    colours[6] = wxColor(128, 128, 0);
    colours[7] = wxColor(192, 192, 0);
    colours[8] = wxColor(128, 128, 128);
    colours[9] =  wxColor(0, 0, 225);
    colours[10] =wxColor(0, 255, 0);
    colours[11] =wxColor(0, 255,255);
    colours[12] =wxColor(255, 0, 0);
    colours[13] =wxColor(255, 0,255);
    colours[14] =wxColor(255, 255, 0);
    colours[15] =wxColor(255, 255,255);
    
    for(int i = 0; i < 16; i++)
    {
        wxButton* btn = new wxButton(toolbar, 10100 + i, "", wxDefaultPosition, wxSize(40,24), 0);
        btn->SetBackgroundColour(colours[i]);
        btn->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyFrame::OnSelectColor), nullptr, this);
        toolbar->AddControl(btn);
    }
    wxButton* btna = new wxButton(toolbar, 10100 + 16, "Alpha", wxDefaultPosition, wxDefaultSize, 0);
    btna->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyFrame::OnSelectColor), nullptr, this);

    toolbar->AddControl(btna);

    toolbar->Realize();
}



void MyFrame::OnExitMenu(wxCommandEvent& evt)
{
    Close();
    evt.Skip();
}
void MyFrame::OnNewMenu(wxCommandEvent& evt)
{
    childFrame* cFrame = new childFrame(this, "Name");
    cFrame->Show();
    evt.Skip();
    
}
void MyFrame::OnSelectColor(wxCommandEvent& evt)
{


}



childFrame.h

#pragma once
#include <wx/wx.h>

class childFrame:wxMDIChildFrame
{

public:
    childFrame(wxMDIParentFrame* parent, wxString childTitle);
    
};


childFrame.cpp

#include "childFrame.h"
#include<wx/wx.h>


childFrame::childFrame(wxMDIParentFrame* parent, wxString ctitle)
    :wxMDIChildFrame(parent,wxID_ANY,ctitle)
{
    
}

Here is the error message

1>D:\Projects\visualStudio\cppStuff\wxWidget\learnWxWidget\learnWxWidget\MyFrame.cpp(72,10): error C2247: 'wxMDIChildFrame::Show' not accessible because 'childFrame' uses 'private' to inherit from 'wxMDIChildFrame'
1>C:\WxWidgets\include\wx\msw\mdi.h(209,18): message : see declaration of 'wxMDIChildFrame::Show'
1>D:\Projects\visualStudio\cppStuff\wxWidget\learnWxWidget\learnWxWidget\childFrame.h(4,7): message : see declaration of 'childFrame'
1>C:\WxWidgets\include\wx\msw\mdi.h(175,24): message : see declaration of 'wxMDIChildFrame'
1>D:\Projects\visualStudio\cppStuff\wxWidget\learnWxWidget\learnWxWidget\MyFrame.cpp(72,10): error C2248: 'wxMDIChildFrame::Show': cannot access private member declared in class 'childFrame'
1>C:\WxWidgets\include\wx\msw\mdi.h(209,18): message : see declaration of 'wxMDIChildFrame::Show'
1>D:\Projects\visualStudio\cppStuff\wxWidget\learnWxWidget\learnWxWidget\childFrame.h(4,7): message : see declaration of 'childFrame'
1>childFrame.cpp
1>Generating Code...

Solution

  • I think you want to change this:

    class childFrame:wxMDIChildFrame
    

    to this:

    class childFrame : public wxMDIChildFrame
    

    ... because if you don't explicitly specify public inheritance, then you get private-inheritance by default, which means that calling code won't be allowed to call methods from the wxMDIChildFrame class's API on an object of type childFrame.