Example.h:2の.CPPファイルでこのヘッダーファイル(ヘッダーガードあり)を含むと、名前付けの衝突が発生するのはなぜですか?
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include "stdafx.h"
#include <Windows.h>
namespace Test
{
DWORD foo;
}
#endif
Example.cpp:
#include "stdafx.h"
#include "Example.h"
Example2.cpp:
#include "stdafx.h"
#include "Example.h"
MAIN.CPP:
#include "stdafx.h"
int main()
{
return 0;
}
これがもたらしますリンカーエラー:
error LNK1169: one or more multiply defined symbols found
error LNK2005: "unsigned long Test::foo" ([email protected]@@3KA) already defined in Example.obj
"Example.h"が "Example2.cpp"に含まれていない場合、このコードはコンパイルされます。私の理解では、Example.hはこの例では一度しか含まれません。それが本当であれば、なぜfoo
の名前の衝突がありますか?
おめでとうございます。なぜ、ヘッダーファイルで宣言されたグローバル変数が悪いのかを知りました。私はこれをもっと多くの人々に渡すことができたらいいと思う。 –