我有一个名为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.h
和Bond.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)
{
...
}
任何帮助吗?
“在另一个类似的问题的答案是,这已经是与在包括相同的文件,并在导致构造的许多不同的实现,但使用的#ifndef _UIH命令可以避免的。” - 你非常误解那里的答案。不,'#ifndef _UIH'不能阻止同一个头文件被包含在多个源文件中,也不应该。 – hvd
那它甚至还能做什么? – PanthersFan92
它可以防止在单个源文件中多次包含相同的头文件。 – hvd