wxGetApp() funktioniert nicht
-
Guten Abend,
ich würde gerne wxGetApp() verwenden, um Foo aus der Klasse App in der Klasse MainFrame zu verwenden. Der Pointer MainFrameX soll aber auch vorhanden sein. Wo liegt mein Fehler?
App.h:
#ifndef App_included #define App_included #include <wx/wx.h> #include "MainFrame.h" class App : public wxApp { public: MainFrame* MainFrameX; wxString Foo; public: virtual bool OnInit(); }; DECLARE_APP(App); #endif
App.cpp:
#include "App.h" IMPLEMENT_APP(App); bool App::OnInit() { Foo = "SuperFoo"; MainFrameX = new MainFrame(0L); MainFrameX->SetIcon(wxICON(MainIcon)); MainFrameX->Show(); return true; }
MainFrame.h:
/////////////////////////////////////////////////////////////////////////// // C++ code generated with wxFormBuilder (version Sep 8 2010) // http://www.wxformbuilder.org/ // // PLEASE DO "NOT" EDIT THIS FILE! /////////////////////////////////////////////////////////////////////////// #ifndef __MainFrame__ #define __MainFrame__ #include <wx/string.h> #include <wx/button.h> #include <wx/gdicmn.h> #include <wx/font.h> #include <wx/colour.h> #include <wx/settings.h> #include <wx/sizer.h> #include <wx/frame.h> #include "App.h" /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// /// Class MainFrame /////////////////////////////////////////////////////////////////////////////// class MainFrame : public wxFrame { private: protected: wxButton* Button1; virtual void OnButton1Click(wxCommandEvent& event) { Button1->SetLabel(wxGetApp().Foo); } public: MainFrame(wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxEmptyString, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(300,200), long style = wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL); ~MainFrame(); }; #endif //__MainFrame__
MainFrame.cpp:
/////////////////////////////////////////////////////////////////////////// // C++ code generated with wxFormBuilder (version Sep 8 2010) // http://www.wxformbuilder.org/ // // PLEASE DO "NOT" EDIT THIS FILE! /////////////////////////////////////////////////////////////////////////// #include "MainFrame.h" /////////////////////////////////////////////////////////////////////////// MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxFrame( parent, id, title, pos, size, style ) { this->SetSizeHints( wxDefaultSize, wxDefaultSize ); wxBoxSizer* BoxSizer1; BoxSizer1 = new wxBoxSizer( wxVERTICAL ); Button1 = new wxButton( this, wxID_ANY, wxT("MyButton"), wxDefaultPosition, wxDefaultSize, 0 ); BoxSizer1->Add( Button1, 1, wxEXPAND, 5 ); this->SetSizer( BoxSizer1 ); this->Layout(); this->Centre( wxBOTH ); // Connect Events Button1->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::OnButton1Click ), NULL, this ); } MainFrame::~MainFrame() { // Disconnect Events Button1->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::OnButton1Click ), NULL, this ); }
Fehler:
MainFrame.h:37:31: Fehler: »wxGetApp« wurde in diesem Gültigkeitsbereich nicht definiert
App.h:10:3: Fehler: »MainFrame« bezeichnet keinen TypMfg,
BluePhaz
-
Sobald ich App.h in MainFrame.h inkludiere, lässt sich das Programm nicht mehr kompilieren. Warum? Die App.h hat doch einen Inklude-Guard...Oder inkludiert sich die MainFrame.h trotzdem selber? Wenn ja, wie verhindere ich das?
-
Hallo,
dein Problem wird hier:
http://www.c-plusplus.net/forum/39498
beschrieben (Zirkulare Abhängigkeit, app.h benötigt "Informationen" von mainframe.h, die wiederum, wegen Verwendung von wxGetApp, "Informationen" aus app.h haben will, die wiederum, siehe oben...), Lösung: Vorwärtsdeklaration
MfG,
Probe-Nutzer