2016-06-13 91 views
1

我有一個名爲UIHandling.h的頭文件中稱爲UIHandling的類。 在班上名列前茅我確信使用方法:錯誤LNK2005:構造函數已定義

#ifndef _UIH_ 
#define _UIH_ 

當然,結束了與#endif

文件這個頭文件由構造函數的所有實現。 我已經包括在我的計劃,但由於某種原因,這個類在許多文件,我得到以下編譯器錯誤:

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 

所以我去Bond.hBond.cpp,看看是否有什麼奇怪的東西(像的實現UIHandling::UIHandling()或類似的東西),並沒有。

我在另一個問題中看到,當你違反ODR這個錯誤,但我沒有。 In another similar question答案是,這與一遍又一遍地包含相同的文件導致構造函數的許多不同實現有關,但使用#ifndef _UIH命令可以避免這種情況。

它可能有一些做我如何聲明和定義構造函數我: 在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) 
{ 
... 
} 

任何幫助嗎?

+0

「在另一個類似的問題的答案是,這已經是與在包括相同的文件,並在導致構造的許多不同的實現,但使用的#ifndef _UIH命令可以避免的。」 - 你非常誤解那裏的答案。不,'#ifndef _UIH'不能阻止同一個頭文件被包含在多個源文件中,也不應該。 – hvd

+0

那它甚至還能做什麼? – PanthersFan92

+0

它可以防止在單個源文件中多次包含相同的頭文件。 – hvd

回答

0

如果您要在類之外和標題中定義成員函數,則需要使用關鍵字inline,並確保每個翻譯單元僅包含一次標題的內容(即每個cpp文件包含一次,這是通過標題完成的包括警衛或#pragma once)。

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

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

該cppreference爲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

爲什麼?爲什麼我只要在頂部有'#pragma once'或'#ifndef'多少次就可以包含'UIHandling.h'多少次? – PanthersFan92

+0

@ PanthersFan92。你可以,它強調的是內容。我會編輯它以使其更清楚。如果內容已經存在於TU中,則包含警衛將確保內容被刪除。 – Niall

+0

但我確實使用'#ifndef',爲什麼我需要使用'inline'?那些實現不會發生兩次。 – PanthersFan92