C++ 首先在这里定义c++;添加一个文件时出错
我有一个非常有趣的错误,我很难根除它。也许这里有人能给我们点启示 所以我有3个文件,一个头文件和2个源文件。我的头文件包含我的类定义。我的一个源文件包含所有类实现。我决定创建一个新文件,以帮助组织我的源代码,并为其提供一些结构 当我把所有代码都放在一个源文件中时,它就可以很好地编译了 但是,当我创建新的源文件并在include语句中添加时:C++ 首先在这里定义c++;添加一个文件时出错,c++,debugging,gcc,compiler-errors,C++,Debugging,Gcc,Compiler Errors,我有一个非常有趣的错误,我很难根除它。也许这里有人能给我们点启示 所以我有3个文件,一个头文件和2个源文件。我的头文件包含我的类定义。我的一个源文件包含所有类实现。我决定创建一个新文件,以帮助组织我的源代码,并为其提供一些结构 当我把所有代码都放在一个源文件中时,它就可以很好地编译了 但是,当我创建新的源文件并在include语句中添加时: #include "UI/OmniFEM.h" 我收到以下错误 ./Debug/UI_mainOmniFEMUI.cpp.o:/home/philm/Gi
#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_事件表'
./Debug/UI_mainOmniFEMUI.cpp.o:(.rodata+0x30):首先在这里定义
./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:首先在这里定义
./Debug/UI_test.cpp.o:在函数中wxEventTableEntry':
/usr/include/wx-3.0-non-official/wx/event.h:3201:OmniFEMMainFrame::sm_eventHashTable的多重定义
./Debug/UI_mainOmniFEMUI.cpp.o:/usr/include/c++/4.8/ext/atomicity.h:49:首先在这里定义
./Debug/UI_test.cpp.o:在函数中wxWindowBase::CanBeFocused()const':
/home/philm/GitHub/Omni FEM//Include/UI/OmniFEM.h:57:omnifemmaframe::sm_eventTableEntries的多重定义
./Debug/UI_mainOmniFEMUI.cpp.o:/home/philm/GitHub/Omni FEM//Include/UI/OmniFEM.h:57:首先在这里定义
./Debug/UI_test.cpp.o:在函数中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:首先在这里定义
./Debug/UI_test.cpp.o:In functionmain':
/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:首先在这里定义
./Debug/UI_test.cpp.o:在函数中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:首先在这里定义
./Debug/UI_test.cpp.o:In functionwxMDIParentFrameBase::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语句的一行。但是,如果它对社区有帮助,请让我知道,我将发布类实现的源代码
对于那些好奇的人,我正在使用Ubuntu14.04和CodeLiteIDE
编辑:
根据用户请求,以下是我的其他源文件的代码:
#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_ */
\ifndef OMNIFEM\u H_
#定义OMNIFEM_H_
#包括
OmniFEMApp类:公共wxApp
{
公众:
虚拟bool-OnInit();
};
类:公共wxFrame
{
公众:
OmniFEMMainFrame(常量wxString和title、常量wxPoint和pos、常量wxSize和size);
私人:
/*此部分用于“文件”菜单*/
作废文件(wxCommandEvent&event);
保存无效(wxCommandEvent&event);
Savas无效(wxCommandEvent&event);
/*此部分用于“编辑”菜单*/
void onPreferences(wxCommandEvent和event);
/*此部分用于“帮助”菜单*/
手动无效(wxCommandEvent&event);
仅许可无效(wxCommandEvent&event);
关于(wxCommandEvent&event)的无效;
无效OnExit(wxCommandEvent&event);
wxDECLARE_事件_表();
};
枚举
{
ID_New=1,
ID_Save=2,
ID_SaveAs=3,
ID_首选项=4,
ID_手动=5,
ID_许可证=6
};
wxBEGIN_事件_表(OmniFEMMainFrame,wxFrame)
/*本节用于文件菜单*/
EVT_菜单(ID_新建,OmniFEMMainFrame::onNewFile)
EVT_菜单(ID_保存,OmniFEMMainFrame::OnSave)
EVT_菜单(ID_SaveAs,OmniFEMMainFrame::onSaveAs)
/*此部分用于查看菜单*/
EVT_菜单(ID_首选项,OmniFEMMainFrame::onPreferences)
/*此部分用于“帮助”菜单*/
EVT_菜单(ID_手动,OmniFEMMainFrame::onManual)
EVT_菜单(ID_许可证,OmniFEMMainFrame::onLicense)
EVT_菜单(wxID_关于,OmniFEMMainFrame::OnAbout)
/*其他一切*/
EVT_菜单(wxID_退出,OmniFEMMainFrame::OnExit)
wxEND_事件_表()
wxIMPLEMENT_应用程序(OmniFEMApp);
#endif/*OMNIFEM_H_*/
好了,伙计们,在另一个论坛上提问后,问题已经解决了
基本上,代码
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文件中,而不是头文件中。这解决了这个问题只需一个include语句就可以包含大量代码,包括已经在另一个翻译单元中编译的代码,从而在链接时产生重复。仅仅因为您只有一个include语句,并不意味着您不能获得链接错误。那么,如何在不获得链接器错误的情况下将头文件包含到源文件中呢?似乎您所说的是编译器正在编译两个头文件,问题不在于包含头文件