2016-06-13 92 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