Search code examples
c++pointerswxwidgets

Why is there no instance of the constructor?


I am new to wxWidgets, and am trying to pass in a pointer to my parent window using the this keyword.

Here is the code.

The code for the frames header file

myframe.h

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

class MyFrame:public wxFrame
{
public :
    MyFrame(const wxString& title);
    ~MyFrame();

public:
    wxGauge* gh = nullptr;
};

The code for the definition of the frame class

myframe.cpp

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

MyFrame::MyFrame(const wxString& title) :wxFrame(nullptr, wxID_ANY, title, wxPoint(30, 30), wxSize(600, 600))
{
    wxButton* bt = new wxButton(this);  
}

app.h

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

class App :public wxApp
{
    bool OnInit();
};

app.cpp

#include <wx/wx.h>
#include "App.h"
#include "MyFrame.h"

wxIMPLEMENT_APP(App);

bool App::OnInit()
{
    MyFrame* frame = new MyFrame("GUi");
    frame->Show();
    frame->Center();
    
    return true;
}

Before I run the code, VS give me an error on this line:

wxButton* bt = new wxButton(this);

where there is a red wave under the opening parenthesis.

If I ignore it and run the code, I get this error:

1>D:\Projects\visualStudio\cppStuff\wxWidget\learnWxWidget\learnWxWidget\MyFrame.cpp(9,29): error C2665: 'wxButton::wxButton': no overloaded function could convert all the argument types
1>C:\WxWidgets\include\wx\msw\button.h(82,5): message : could be 'wxButton::wxButton(const wxButton &)'
1>D:\Projects\visualStudio\cppStuff\wxWidget\learnWxWidget\learnWxWidget\MyFrame.cpp(9,29): message : 'wxButton::wxButton(const wxButton &)': cannot convert argument 1 from 'MyFrame *' to 'const wxButton &'
1>D:\Projects\visualStudio\cppStuff\wxWidget\learnWxWidget\learnWxWidget\MyFrame.cpp(9,30): message : Reason: cannot convert from 'MyFrame *' to 'const wxButton'
1>D:\Projects\visualStudio\cppStuff\wxWidget\learnWxWidget\learnWxWidget\MyFrame.cpp(9,29): message : while trying to match the argument list '(MyFrame *)'
1>Done building project "learnWxWidget.vcxproj" -- FAILED.

Solution

  • Per the wxWidgets documentation, wxButton does not have a constructor that accepts only a parent window as a parameter. At the very least, you need to also provide an id for your button, just like you are doing with your wxFrame, eg:

    wxButton* bt = new wxButton(this, desiredIdHere);