可能重复:
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。 –