2012-08-27 119 views
2

我想使用WxWidgets庫將GUI添加到我的程序中。我也有一個頭文件,我把所有的#include語句放在那裏(「All_Headers.h」)。WxWidgets符號解析和鏈接

我在頭文件(「GUI.h」)中添加了一個包含簡單WxFrame(Hello world like)的頭文件。

的問題是,如果我把(#包括 「GUI.h」)在All_Headers.h我得到以下錯誤: (Kernel.obj和Crystall_Builder.obj是我的目標文件)

1>Kernel.obj : error LNK2005: "protected: static struct wxEventTable const MyFrame::sm_eventTable" ([email protected]@@[email protected]@B) already defined in Crystall_Builder.obj 
    1>Kernel.obj : error LNK2005: "protected: virtual struct wxEventTable const * __cdecl MyFrame::GetEventTable(void)const " ([email protected]@@[email protected]@XZ) already defined in Crystall_Builder.obj 
    1>Kernel.obj : error LNK2005: "protected: virtual class wxEventHashTable & __cdecl MyFrame::GetEventHashTable(void)const " ([email protected]@@[email protected]@XZ) already defined in Crystall_Builder.obj 
    1>Kernel.obj : error LNK2005: "public: __cdecl MyFrame::MyFrame(class wxString const &,class wxPoint const &,class wxSize const &)" ([email protected]@[email protected]@@[email protected]@[email protected]@@Z) already defined in Crystall_Builder.obj 
    1>Kernel.obj : error LNK2005: "public: void __cdecl MyFrame::OnQuit(class wxCommandEvent &)" ([email protected]@@[email protected]@@Z) already defined in Crystall_Builder.obj 
    1>Kernel.obj : error LNK2005: "public: void __cdecl MyFrame::OnAbout(class wxCommandEvent &)" ([email protected]@@[email protected]@@Z) already defined in Crystall_Builder.obj 

但是,如果我把(#包括「GUI.h」)在主文件(Kernel.cpp)它工作正常!

我沒有任何想法我怎麼能找到這個錯誤的起源,請幫助。

非常感謝

主文件(Kernel.cpp)

// #include "GUI.h" // If I put GUI.h in here it works fine 
#include "All_Headers.h" 
IMPLEMENT_APP(MyApp) 
bool MyApp::OnInit() 
{ 
    MyFrame *frame = new MyFrame("Hello World", 
     wxPoint(50,50), wxSize(450,340)); 
    frame->Show(TRUE); 
    SetTopWindow(frame); 
    return TRUE; 
} 

GUI.h:

#ifndef GUI_H 
#define GUI_H 

#include "wx/wx.h" 


class MyApp: public wxApp 
{ 
    virtual bool OnInit(); 
}; 


class MyFrame: public wxFrame 
{ 
public: 

    MyFrame(const wxString& title, 
      const wxPoint& pos, const wxSize& size); 

    void OnQuit(wxCommandEvent& event); 
    void OnAbout(wxCommandEvent& event); 

    DECLARE_EVENT_TABLE() 
}; 

enum 
{ 
    ID_Quit = 1, 
    ID_About, 

}; 

BEGIN_EVENT_TABLE(MyFrame, wxFrame) 
    EVT_MENU(ID_Quit, MyFrame::OnQuit) 
    EVT_MENU(ID_About, MyFrame::OnAbout) 
END_EVENT_TABLE() 

MyFrame::MyFrame(const wxString& title, 
     const wxPoint& pos, const wxSize& size) 
: wxFrame((wxFrame *)NULL, -1, title, pos, size) 
{ 
    wxMenu *menuFile = new wxMenu; 
    menuFile->Append(ID_About, "&About..."); 
    menuFile->AppendSeparator(); 
    menuFile->Append(ID_Quit, "E&xit"); 

    wxMenuBar *menuBar = new wxMenuBar; 
    menuBar->Append(menuFile, "&File"); 

    SetMenuBar(menuBar); 

    CreateStatusBar(); 
    SetStatusText("Welcome to wxWindows!"); 
} 


void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) 
{ 
    Close(TRUE); 
} 

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) 
{ 

    wxMessageBox("This is a wxWindows Hello world sample", 
     "About Hello World", wxOK | wxICON_INFORMATION, this); 
} 

#endif 

回答

5

BEGIN_EVENT_TABLE()啓動事件表定義,所以它必須在源文件中,而不是在標題中(t不出意外,他在DECLARE_EVENT_TABLE()的事件表聲明)。

+0

謝謝,問題已解決,但您能否解釋它爲什麼應該在源文件中,以及爲什麼會出現鏈接錯誤。謝謝 – Hesam

+1

鏈接錯誤的出現是因爲你有多個C++中禁止的同一事件(事件表)的定義(和C有關)。 –