2

我正在尝试使用 WxWidgets 库将 GUI 添加到我的程序中。我还有一个头文件,我将所有#include 语句放在那里(“All_Headers.h”)。

我在头文件(“GUI.h”)中添加了一个包含简单 WxFrame(类似 Hello world)的头文件。

问题是如果我将 (#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;
} 

图形用户界面.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 
4

1 回答 1

5

BEGIN_EVENT_TABLE()启动事件表定义,所以它必须在源文件中,而不是在头文件中(事件表声明在里面,不出所料DECLARE_EVENT_TABLE())。

于 2012-08-27T10:08:07.190 回答