'&': illegal operation on bound member function expression

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Shadowblitz16
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sun Oct 13, 2019 2:32 am

'&': illegal operation on bound member function expression

Post by Shadowblitz16 » Sun Oct 13, 2019 2:41 am

hello I am new to c++ and wxwidgets and was messing around with them to learn them, however I keep getting the error

Code: Select all

'&`: illegal operation on bound member function expression`
with the following code..

Code: Select all

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

class MainWindow : public wxFrame
{
	public:
		//Constructor and Deconstructor
		 MainWindow(const wxString& title);
		~MainWindow();

		//wxWidgets
		wxButton* buttonAdd;
		wxButton* buttonSub;
		wxStaticText* label;

		//Events
		void ButtonAddClicked(wxCommandEvent & event);
	    void ButtonSubClicked(wxCommandEvent & event);

	private:
		int count;
};

Code: Select all

//MainWindow.cpp
#include "MainWindow.h"
#include <wx/artprov.h>



MainWindow::MainWindow(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))
{
	wxMenuBar* menuBar = new wxMenuBar();
	wxMenu*    menu1   = new wxMenu();

	count = 0;

	//wxWidgets default wxMenuItem example
	menu1->Append(wxID_ANY, _("&New\tCtrl+N"  ));
	menu1->Append(wxID_ANY, _("&Open\tCtrl+O" ));
	menu1->Append(wxID_ANY, _("&Save\tCtrl+S" ));
	menu1->Append(wxID_ANY, _("&Quit\tCtrl+Q" ));

	menuBar->Append(menu1, _("&File" ));
	SetMenuBar(menuBar);

	SetBackgroundColour(wxSystemSettings::GetColour(wxSystemColour::wxSYS_COLOUR_WINDOW));
	buttonAdd = new wxButton    (this, wxID_ANY, wxT("+"), wxPoint(24, 24), wxSize(24, 24));
	buttonSub = new wxButton    (this, wxID_ANY, wxT("-"), wxPoint(48, 24), wxSize(24, 24));
	label     = new wxStaticText(this, wxID_ANY, wxT("" ), wxPoint(72, 24), wxSize(24, 96));

	Connect(buttonAdd->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::ButtonAddClicked));
	Connect(buttonSub->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::ButtonSubClicked));

}


MainWindow::~MainWindow()
{
}

void MainWindow::ButtonAddClicked(wxCommandEvent & WXUNUSED(event))
{
	count++;
	label->SetLabel(wxString::Format(wxT("%d"), count));
}

void MainWindow::ButtonSubClicked(wxCommandEvent & WXUNUSED(event))
{
	count--;
	label->SetLabel(wxString::Format(wxT("%d"), count));
}
I am using visual studio 2019 and was following this tutorial.. http://zetcode.com/gui/wxwidgets/firstprograms/

can someone explain to me whats wrong?

catalin
Moderator
Moderator
Posts: 1590
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: '&': illegal operation on bound member function expression

Post by catalin » Sun Oct 13, 2019 8:16 am

Everything looks fine with your code. The error must be coming from somewhere else. You should provide more information about an error, like the file and line number where it occurred.
Or even better: debug it yourself, by removing pieces of code one by one until it compiles.

Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Re: '&': illegal operation on bound member function expression

Post by Wolfgang » Sun Oct 13, 2019 8:40 am

There is no error shown, but it does not show the popup window.
Just thinking,could it be that another event catches the click, have to check that one.

catalin
Moderator
Moderator
Posts: 1590
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: '&': illegal operation on bound member function expression

Post by catalin » Sun Oct 13, 2019 8:45 am

Shadowblitz16 wrote:
Sun Oct 13, 2019 2:41 am
I keep getting the error
Wolfgang wrote:
Sun Oct 13, 2019 8:40 am
There is no error shown
No error shown, but then how do you get it?

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2517
Joined: Sun Jan 03, 2010 5:45 pm

Re: '&': illegal operation on bound member function expression

Post by PB » Sun Oct 13, 2019 9:50 am

I strongly suggest using Bind() instead of Connect(). Connect() is de-facto deprecated, the only reason it is not deprecated officially is that lots of old code and even code produced by RAD tools still has it. Not only is Connect() using an ugly cast, it is also error-prone to accidentally using a wrong event sink.

So, instead of

Code: Select all

Connect(buttonAdd->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::ButtonAddClicked));
you should use

Code: Select all

buttonAdd->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MainWindow::ButtonAddClicked, this);
Notice that now there is a "&" in front of the event handler method name (it is a pointer) and the last parameter is "this", meaning the instance for handling the even is the MainWindow.

You also do not need to use wxT() macros, these are also a remnant of the past.

And last, but certainly not least, using hard-coded positions and sizes for controls is also a bad idea for several reasons. Sizers are the way to go.

Shadowblitz16
Earned a small fee
Earned a small fee
Posts: 10
Joined: Sun Oct 13, 2019 2:32 am

Re: '&': illegal operation on bound member function expression

Post by Shadowblitz16 » Sun Oct 13, 2019 6:17 pm

I just opened the project and built it today and its working with no errors?
does anybody know why this could happen?

I literally haven't changed anything and its up to date and works.
PB wrote:
Sun Oct 13, 2019 9:50 am
I strongly suggest using Bind() instead of Connect(). Connect() is de-facto deprecated, the only reason it is not deprecated officially is that lots of old code and even code produced by RAD tools still has it. Not only is Connect() using an ugly cast, it is also error-prone to accidentally using a wrong event sink.

So, instead of

Code: Select all

Connect(buttonAdd->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::ButtonAddClicked));
you should use

Code: Select all

buttonAdd->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MainWindow::ButtonAddClicked, this);
Notice that now there is a "&" in front of the event handler method name (it is a pointer) and the last parameter is "this", meaning the instance for handling the even is the MainWindow.

You also do not need to use wxT() macros, these are also a remnant of the past.

And last, but certainly not least, using hard-coded positions and sizes for controls is also a bad idea for several reasons. Sizers are the way to go.
Bind is looking mighty nice I will definitely use it instead of Connect. wxT() is also a nice thing to get rid of.
also I will look into sizers.
thankyou for the info.

Post Reply