可能重複:
Why does this not prevent multiple function declarations?爲什麼即使使用標頭警衛也會出現鏈接錯誤?
Global.h
#ifndef Global_h
#define Global_h
#include <iostream>
unsigned char exitStatus;
#endif
OutputHandler.h
#ifndef OutputHandler_h
#define OutputHandler_h
#include "Global.h"
class OutputHandler {
private:
static bool instanceExists;
// more code
#endif
Root.h
#ifndef Root_h
#define Root_h
// declarations
OutputHandler *output;
#endif
ROOT.CPP
#include "Root.h"
// gets instance of OutputHandler
// more code
我越來越被已經被在OutputHandler定義Root.obj關於退出狀態,靜態布爾instanceExists,並靜態類輸出錯誤.obj。我認爲問題在於包含頭文件OutputHandler.h都在Root.h和OutputHandler.cpp。任何人都知道如何解決這個問題或如何更好地組織頭文件?
到Q的答案爲什麼在詳細講解。簡而言之,您不應該在頭文件中定義對象,然後將該頭包含在多個TU中。它違反了ODR。 –