我正在尝试使用WxWidgets库将GUI添加到我的程序中。我还有一个头文件,我把所有#include语句放在那里(“All_Headers.h”)。
我添加了一个头文件,其中包含一个简单的WxFrame(Hello world like)到头文件(“GUI.h”)中。
问题是如果我把(#include“GUI.h”)放在All_Headers.h中我收到以下错误: (Kernel.obj和Crystall_Builder.obj是我的目标文件)
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
但是如果我把(#include“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
答案 0 :(得分:5)
BEGIN_EVENT_TABLE()
启动事件表 definition ,因此它必须位于源文件中,而不是标题中(事件表声明不出所料,在DECLARE_EVENT_TABLE()
内)