Wierd bug with event tables on wxFrame derived classes Topic is solved

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
GhostManZero
In need of some credit
In need of some credit
Posts: 8
Joined: Sun Dec 17, 2006 5:23 pm
Location: Portugal
Contact:

Wierd bug with event tables on wxFrame derived classes

Post by GhostManZero » Mon Dec 18, 2006 2:11 pm

Greetings everyone. I was working on my editor, as usually, and i was writing the event table of the main frame.
But, when i do

Code: Select all

BEGIN_EVENT_TABLE(GhostFrame, wxFrame)
EVT_MENU(EVENTID_MAINFRAME_EXIT,GhostFrame::OnExit())
END_EVENT_TABLE()
I get errors on VS2005 like this:

Code: Select all

1>e:\ghostengine\projects\testapp\main.cpp(42) : error C2102: '&' requires l-value
1>e:\ghostengine\projects\testapp\main.cpp(42) : error C2466: cannot allocate an array of constant size 0
1>e:\ghostengine\projects\testapp\main.cpp(42) : error C2440: 'initializing' : cannot convert from 'int' to 'const wxEventTableEntry'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
It is also talking about constructors, but i dont think that has anything to do with my frame, at least i was following a tutorial and it worked fine except when i tried to add the event table.

But anyway, if it helps, here's my GhostFrame class declaration:

Code: Select all

class GhostFrame : public wxFrame
{
public:
    GhostFrame(wxFrame *parent, wxWindowID id = wxID_ANY, const wxString& title = _T("wxToolBar Sample"),
		const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize ) :
	wxFrame(parent, id, title, pos, size )
	{
		wxMenu *menuFile = new wxMenu;

		menuFile->Append( EVENTID_MAINFRAME_EXIT, "E&xit" );
		wxMenuBar *menuBar = new wxMenuBar;
		menuBar->Append( menuFile, "&File" );
		SetMenuBar( menuBar );
		CreateStatusBar();
		SetStatusText( "GhostEngine Scene Graph Editor is loading..." );
	};
	static void OnExit()
	{
		wxExit();
	};
	DECLARE_EVENT_TABLE()
};
I had to declare OnExit() as static, because it wouldnt accept the member otherwise, it'd go with an error like this:

Code: Select all

1>e:\ghostengine\projects\testapp\main.cpp(41) : error C2352: 'GhostFrame::OnExit' : illegal call of non-static member function
1>        e:\ghostengine\projects\testapp\main.cpp(34) : see declaration of 'GhostFrame::OnExit'
Anyone got any ideas on what i should do?
Thank you all for your time regarding this message.

User avatar
doublemax
Moderator
Moderator
Posts: 15512
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Mon Dec 18, 2006 2:15 pm

don't put a function call inside the event table, i.e. remove the () after OnExit

Code: Select all

EVT_MENU(EVENTID_MAINFRAME_EXIT,GhostFrame::OnExit) 
Use the source, Luke!

S.Volkenandt
Knows some wx things
Knows some wx things
Posts: 26
Joined: Tue Nov 14, 2006 1:51 pm
Location: Duesseldorf, Germany

Post by S.Volkenandt » Mon Dec 18, 2006 2:17 pm

You had to make the OnExit method static because you are actually calling it when declaring the event table (notice the empty parens!). Since you are calling it without an object, it must be static for that piece of code to work.

However, I guess you wanted

Code: Select all

EVT_MENU(EVENTID_MAINFRAME_EXIT,GhostFrame::OnExit)
And I guess the OnExit method (besides from having it to be non-static) should take a wxCommandEvent& argument as first parameter.

Code: Select all

void OnExit(wxCommandEvent& args)
{
    // ...
}

GhostManZero
In need of some credit
In need of some credit
Posts: 8
Joined: Sun Dec 17, 2006 5:23 pm
Location: Portugal
Contact:

Post by GhostManZero » Mon Dec 18, 2006 2:20 pm

Thanks a lot you two! You both helped me a lot! ^^
Topic Solved.

JSThePatriot
Earned some good credits
Earned some good credits
Posts: 146
Joined: Sun Nov 26, 2006 7:37 am
Location: Tennessee, USA
Contact:

Post by JSThePatriot » Mon Dec 18, 2006 2:38 pm

Okay there are several things I would recommend doing. It seems you are forgetting some ";"'s (semi-colon). I don't think that is your main problem, but it may be a future problem.

Here is how I would write what you have given us. BTW The other two posts before me have explained very well what needs to happen, I am just offering some tips to help you on your coding experience.

Code: Select all

//This is the class declaration only. The functions are to be defined later.
class GhostFrame : public wxFrame {
    public:
        GhostFrame(wxFrame *parent, wxWindowID id = wxID_ANY, wxString title = _T("wxToolBar Sample"), wxPoint pos = wxDefaultPosition, wxSize size = wxDefaultSize );
    protected:
        wxMenu* menuFile;
        wxMenuBar* menuBar;
    private:
        void OnExit(wxCommandEvent& event);
        DECLARE_EVENT_TABLE();
};

GhostFrame::GhostFrame(wxFrame* parent, wxWindowID id, wxString title, wxPoint pos, wxSize size) : wxFrame(parent, id, title, pos, size ) {
    menuFile = new wxMenu;

    menuFile->Append( EVENTID_MAINFRAME_EXIT, "E&xit" );
    menuBar = new wxMenuBar;
    menuBar->Append( menuFile, "&File" );
    SetMenuBar( menuBar );
    CreateStatusBar();
    SetStatusText( "GhostEngine Scene Graph Editor is loading..." );
}

GhostFrame::OnExit(wxCommandEvent& event) {
    Close(true);
}
That is how I would do it based on my learning from all the reading I have done so far.

I hope that helps,
JS

Post Reply