Ich habe eine Klasse namens UIHandling
in einer Header-Datei namens UIHandling.h
. An der Spitze der Klasse machte ich sicher zu verwenden:Fehler LNK2005: Konstruktor bereits definiert
#ifndef _UIH_
#define _UIH_
Und natürlich endete die Datei mit #endif
Diese Header-Datei, die alle Implementierungen des Konstrukteure besteht. Ich habe diese Klasse gehören in vielen Dateien im Programm aber aus irgendeinem Grund habe ich den Fehler folgende Compiler:
1>CompaniesMap.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(enum eType)" ([email protected]@[email protected]@@@Z) already defined in Bond.obj
1>CompaniesMap.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(void)" ([email protected]@[email protected]) already defined in Bond.obj
1>Company.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(enum eType)" ([email protected]@[email protected]@@@Z) already defined in Bond.obj
1>Company.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(void)" ([email protected]@[email protected]) already defined in Bond.obj
1>Date.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(enum eType)" ([email protected]@[email protected]@@@Z) already defined in Bond.obj
1>Date.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(void)" ([email protected]@[email protected]) already defined in Bond.obj
1>GovStock.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(enum eType)" ([email protected]@[email protected]@@@Z) already defined in Bond.obj
1>GovStock.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(void)" ([email protected]@[email protected]) already defined in Bond.obj
1>main.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(enum eType)" ([email protected]@[email protected]@@@Z) already defined in Bond.obj
1>main.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(void)" ([email protected]@[email protected]) already defined in Bond.obj
1>Stock.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(enum eType)" ([email protected]@[email protected]@@@Z) already defined in Bond.obj
1>Stock.obj : error LNK2005: "public: __thiscall UIHandling::UIHandling(void)" ([email protected]@[email protected]) already defined in Bond.obj
1>D:\Asaf\C\VS\hw5\HW5\Debug\HW5.exe : fatal error LNK1169: one or more multiply defined symbols found
Also ging ich zu Bond.h
und Bond.cpp
zu sehen, ob es gibt etwas seltsam (wie eine Implementierung UIHandling::UIHandling()
oder so ähnlich) und da ist es nicht.
Ich sah in einer anderen Frage, dass dieser Fehler zeigt, wenn Sie die ODR verletzen, aber ich habe nicht. In another similar question Die Antwort war, dass dies etwas damit zu tun hat, die gleiche Datei immer wieder zu verwenden, was viele verschiedene Implementierungen des Konstruktors verursacht, aber das wird mit dem Befehl #ifndef _UIH
vermieden.
Es kann etwas mit dem zu tun haben, wie ich erklären und meine Konstrukteure definieren: In UIHandling.h
:
class UIHandling : public exception
{
public:
UIHandling(); // Default C'tor - error unknown
UIHandling(eType); // C'tor with error type
template <class T>
UIHandling(eType, T); // C'tor with error type and relevant number
...
}
...
UIHandling::UIHandling()
{
...
}
UIHandling::UIHandling(eType e)
{
...
}
template <class T>
UIHandling::UIHandling(eType e, T number)
{
...
}
Hilfe?
„In einer anderen ähnlichen Frage war die Antwort, dass dies etwas mit, einschließlich der gleiche Akte über zu tun hat und über viele verschiedene Implementierungen des Konstrukteurs zu verursachen, aber das wird mit dem #ifndef _UIH Befehl vermieden.“ - Sie verstehen die Antworten dort sehr falsch. Nein, '#ifndef _UIH' verhindert nicht, dass dieselbe Header-Datei in mehreren Quelldateien enthalten ist. – hvd
Was macht es überhaupt? – PanthersFan92
Es verhindert, dass die gleiche Header-Datei mehrfach in einer einzigen Quelldatei enthalten ist. – hvd