I’m trying to add GUI to my program using WxWidgets library. I also have a header file which I put all the #include statements there (“All_Headers.h”).
I have added a header file which contains a simple WxFrame (Hello world like) into a header file (“GUI.h”).
The problem is if I put (#include “GUI.h”) in the All_Headers.h I get the following error:
(Kernel.obj and Crystall_Builder.obj are my object files)
1>Kernel.obj : error LNK2005: "protected: static struct wxEventTable const MyFrame::sm_eventTable" (?sm_eventTable@MyFrame@@1UwxEventTable@@B) already defined in Crystall_Builder.obj
1>Kernel.obj : error LNK2005: "protected: virtual struct wxEventTable const * __cdecl MyFrame::GetEventTable(void)const " (?GetEventTable@MyFrame@@MEBAPEBUwxEventTable@@XZ) already defined in Crystall_Builder.obj
1>Kernel.obj : error LNK2005: "protected: virtual class wxEventHashTable & __cdecl MyFrame::GetEventHashTable(void)const " (?GetEventHashTable@MyFrame@@MEBAAEAVwxEventHashTable@@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 &)" (??0MyFrame@@QEAA@AEBVwxString@@AEBVwxPoint@@AEBVwxSize@@@Z) already defined in Crystall_Builder.obj
1>Kernel.obj : error LNK2005: "public: void __cdecl MyFrame::OnQuit(class wxCommandEvent &)" (?OnQuit@MyFrame@@QEAAXAEAVwxCommandEvent@@@Z) already defined in Crystall_Builder.obj
1>Kernel.obj : error LNK2005: "public: void __cdecl MyFrame::OnAbout(class wxCommandEvent &)" (?OnAbout@MyFrame@@QEAAXAEAVwxCommandEvent@@@Z) already defined in Crystall_Builder.obj
However if I put (#include “GUI.h”) in the Main file (Kernel.cpp) it works fine!
I don’t have any Idea how I can find the origin of this error, please help.
Many Thanks
Main file (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
BEGIN_EVENT_TABLE()starts the event table definition, so it must be in a source file, not in a header (the event table declaration is, unsurprisingly, insideDECLARE_EVENT_TABLE()).