2016-06-13 14 views
1

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?

+0

„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

+0

Was macht es überhaupt? – PanthersFan92

+0

Es verhindert, dass die gleiche Header-Datei mehrfach in einer einzigen Quelldatei enthalten ist. – hvd

Antwort

0

Wenn Sie die Elementfunktionen außerhalb der Klasse und in der Kopfzeile definieren, müssen Sie das Schlüsselwort inline verwenden und sicherstellen, dass der Inhalt der Kopfzeile nur einmal pro Übersetzungseinheit enthalten ist (dh pro cpp-Datei mit Kopfzeile) schließen Wächter oder #pragma once ein).

class UIHandling : public exception 
{ 
public: 
    UIHandling(); 
    // ... 
}; 

inline // << add this... 
UIHandling::UIHandling() 
{ 
} 

Die cppreference für inline;

An inline function is a function with the following properties:

  1. There may be more than one definition of an inline function in the program as long as each definition appears in a different translation unit. For example, an inline function may be defined in a header file that is #include'd in multiple source files.
  2. The definition of an inline function must be present in the translation unit where it is called (not necessarily before the point of call).
+0

Warum? Warum kann ich 'UIHandling.h' nicht so oft einfügen, wie es" #pragma once "oder" # ifndef "an der Spitze hat? – PanthersFan92

+0

@ PanthersFan92. Sie können, es ist der Schwerpunkt auf den Inhalt. Ich werde es bearbeiten, um es klarer zu machen. Die Include-Wächter stellen sicher, dass der Inhalt entfernt wird, wenn er bereits in der TU vorhanden ist. – Niall

+0

Aber ich habe '# ifndef' verwendet, warum brauche ich' inline'? Diese Implementierungen werden nicht zweimal vorkommen. – PanthersFan92

Verwandte Themen