首先通过添加一个文件

时间:2016-03-18 02:22:10

标签: c++ debugging gcc compiler-errors

我有一个非常有趣的错误,我很难根除。也许这里有人可以解释一下。

所以我有3个文件,一个头文件和两个源文件。我的头文件包含我的类定义。我的一个源文件包含所有类实现。我决定创建一个新文件,以帮助我更多地组织我的源代码并给它一些结构。

当我将所有代码放在一个源文件中时,它编译得很好。

但是,当我创建一个新的源文件并添加include语句时:

#include "UI/OmniFEM.h"

我收到以下错误

  

./ Debug / UI_mainOmniFEMUI.cpp.o:/ home / philm / GitHub / Omni-FEM /./ Include / UI / OmniFEM.h:57:首先在这里定义   ./Debug/UI_test.cpp.o:(.rodata+0x30):OmniFEMMainFrame::sm_eventTable' ./Debug/UI_mainOmniFEMUI.cpp.o:(.rodata+0x30): first defined here ./Debug/UI_test.cpp.o: In function OmniFEMMainFrame :: GetEventHashTable()const'的多重定义:   /home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:57: OmniFEMMainFrame::GetEventHashTable() const' ./Debug/UI_mainOmniFEMUI.cpp.o:/home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:57: first defined here ./Debug/UI_test.cpp.o: In function wxEventTableEntry'的多重定义:   /usr/include/wx-3.0-unofficial/wx/event.h:3201:OmniFEMMainFrame::sm_eventHashTable' ./Debug/UI_mainOmniFEMUI.cpp.o:/usr/include/c++/4.8/ext/atomicity.h:49: first defined here ./Debug/UI_test.cpp.o: In function wxWindowBase :: CanBeFocused()const'的多重定义:   /home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:57: OmniFEMMainFrame::sm_eventTableEntries' ./Debug/UI_mainOmniFEMUI.cpp.o:/home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:57: first defined here ./Debug/UI_test.cpp.o: In function wxMDIParentFrameBase :: OnCreateClient()'的多重定义:   /home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:76:wxCreateApp()' ./Debug/UI_mainOmniFEMUI.cpp.o:/home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:76: first defined here ./Debug/UI_test.cpp.o: In function main'的多重定义:   /home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:76:main' ./Debug/UI_mainOmniFEMUI.cpp.o:/home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:76: first defined here ./Debug/UI_test.cpp.o: In function wxGetApp()'的多重定义:   /home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:76:wxGetApp()' ./Debug/UI_mainOmniFEMUI.cpp.o:/home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:76: first defined here ./Debug/UI_test.cpp.o: In function wxMDIParentFrameBase :: ArrangeIcons()'的多重定义:   /home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:57:`wxTheAppInitializer'的多重定义   ./Debug/UI_mainOmniFEMUI.cpp.o:/home/philm/GitHub/Omni-FEM/./Include/UI/OmniFEM.h:57:首先在这里定义

关键是这个,我收到多个首先定义的错误。我不确定为什么,因为我在源文件中只有一行代码,它是头文件的include语句。当我删除这个语句时,代码再次编译就好了。

我没有发现有必要发布代码,因为源文件是空的,除了include语句的单行。但是,如果它对社区有帮助,请告诉我,我将发布课程实施的来源。

对于那些好奇的人,我使用的是ubuntu 14.04和codelite IDE。

编辑:

根据用户请求,这是我的其他源文件的代码:

#include "UI/OmniFEM.h"


bool OmniFEMApp::OnInit()
{
   OmniFEMMainFrame *frame = new OmniFEMMainFrame("Omni-FEM", wxPoint(50, 50), wxSize(450, 340) );
   frame->Show( true );
   return true; 
}


OmniFEMMainFrame::OmniFEMMainFrame(const wxString &title, const wxPoint &pos, const wxSize &size) : wxFrame(NULL, wxID_ANY, title, pos, size)
{
    /* Initilize variables */
    wxMenuBar *menuBar = new wxMenuBar;
    wxMenu *menuFile = new wxMenu;
    wxMenu *menuEdit = new wxMenu;
    wxMenu *menuView = new wxMenu;
    wxMenu *menuMesh = new wxMenu;
    wxMenu *menuProblem = new wxMenu;
    wxMenu *menuHelp = new wxMenu;

    /* This creates the main menu Bar at the top */
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuEdit, "&Edit");
    menuBar->Append(menuView, "&View");
    menuBar->Append(menuMesh, "&Mesh");
    menuBar->Append(menuProblem, "&Problem");
    menuBar->Append(menuHelp, "&Help");


    /* Creating the menu listing of File menu */
    menuFile->Append(ID_New, "&New\tCtrl-N");
    menuFile->Append(ID_Save, "&Save\tCtrl-S");
    menuFile->Append(ID_SaveAs, "&Save As");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);

    /* Creating the menu listinging of the Edit Menu */
    menuEdit->Append(ID_Preferences, "&Preferences\tCtrl-P");

    /* Creates the menu listing of the help menu */
    menuHelp->Append(ID_Manual, "View Manual");
    menuHelp->AppendSeparator();
    menuHelp->Append(ID_License, "License");
    menuHelp->Append(wxID_ABOUT);

    /* Create and display the menu bar */
    SetMenuBar(menuBar);
    CreateStatusBar();

    SetStatusText("Menu test for Omni-FEM");
}



void OmniFEMMainFrame::OnExit(wxCommandEvent &event)
{
    Close(true);
}



void OmniFEMMainFrame::onNewFile(wxCommandEvent &event)
{
    wxMessageBox("Created New File", "New File Creation", wxOK | wxICON_INFORMATION);
}



void OmniFEMMainFrame::OnSave(wxCommandEvent &event)
{
    wxMessageBox("Work saved", "Save", wxOK | wxICON_INFORMATION);
}



void OmniFEMMainFrame::onSaveAs(wxCommandEvent &event)
{
    wxMessageBox("Work saved in location", "Saved As", wxOK | wxICON_INFORMATION);
}



void OmniFEMMainFrame::onPreferences(wxCommandEvent &event)
{
    wxMessageBox("Preferences are located here", "Preferences", wxOK | wxICON_INFORMATION);
}


void OmniFEMMainFrame::OnAbout(wxCommandEvent &event)
{
    wxMessageBox("This is a test", "New File", wxOK | wxICON_INFORMATION);
}

void OmniFEMMainFrame::onManual(wxCommandEvent &event)
{
    wxMessageBox("This is the manual", "Manual", wxOK | wxICON_INFORMATION);
}

void OmniFEMMainFrame::onLicense(wxCommandEvent &event)
{
    wxMessageBox("This is the license", "License", wxOK | wxICON_INFORMATION);
}

以下是头文件的代码:

#ifndef OMNIFEM_H_
#define OMNIFEM_H_

#include <wx/wx.h>

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



class OmniFEMMainFrame : public wxFrame
{
public:
    OmniFEMMainFrame(const wxString &title, const wxPoint &pos, const wxSize &size);
private:
    /* This section is for the File menu */
    void onNewFile(wxCommandEvent &event);
    void OnSave(wxCommandEvent &event);
    void onSaveAs(wxCommandEvent &event);

    /* This section is for the Edit menu */
    void onPreferences(wxCommandEvent &event);

    /* This section is for the Help menu */
    void onManual(wxCommandEvent &event);
    void onLicense(wxCommandEvent &event);
    void OnAbout(wxCommandEvent &event);

    void OnExit(wxCommandEvent &event);
    wxDECLARE_EVENT_TABLE();
};



enum
{
    ID_New = 1,
    ID_Save = 2,
    ID_SaveAs = 3,
    ID_Preferences = 4,
    ID_Manual = 5,
    ID_License = 6
};



wxBEGIN_EVENT_TABLE(OmniFEMMainFrame, wxFrame)
    /* This section is for teh file menu */
    EVT_MENU(ID_New,   OmniFEMMainFrame::onNewFile)
    EVT_MENU(ID_Save, OmniFEMMainFrame::OnSave)
    EVT_MENU(ID_SaveAs, OmniFEMMainFrame::onSaveAs)

    /* This section is for the view menu */
    EVT_MENU(ID_Preferences, OmniFEMMainFrame::onPreferences)

    /* This section is for the Help menu */
    EVT_MENU(ID_Manual, OmniFEMMainFrame::onManual)
    EVT_MENU(ID_License, OmniFEMMainFrame::onLicense)
    EVT_MENU(wxID_ABOUT, OmniFEMMainFrame::OnAbout)

    /* Everything Else */
    EVT_MENU(wxID_EXIT,  OmniFEMMainFrame::OnExit)

wxEND_EVENT_TABLE()

wxIMPLEMENT_APP(OmniFEMApp);


#endif /* OMNIFEM_H_ */

1 个答案:

答案 0 :(得分:1)

好的,在另一个论坛上提问之后,问题已经解决了。

基本上,代码

wxBEGIN_EVENT_TABLE(OmniFEMMainFrame, wxFrame)
    /* This section is for teh file menu */
    EVT_MENU(ID_New,   OmniFEMMainFrame::onNewFile)
    EVT_MENU(ID_Save, OmniFEMMainFrame::OnSave)
    EVT_MENU(ID_SaveAs, OmniFEMMainFrame::onSaveAs)

    /* This section is for the view menu */
    EVT_MENU(ID_Preferences, OmniFEMMainFrame::onPreferences)

    /* This section is for the Help menu */
    EVT_MENU(ID_Manual, OmniFEMMainFrame::onManual)
    EVT_MENU(ID_License, OmniFEMMainFrame::onLicense)
    EVT_MENU(wxID_ABOUT, OmniFEMMainFrame::OnAbout)

    /* Everything Else */
    EVT_MENU(wxID_EXIT,  OmniFEMMainFrame::OnExit)

wxEND_EVENT_TABLE()

wxIMPLEMENT_APP(OmniFEMApp);

需要放在.cpp文件中而不是头文件中。这解决了问题