看来我只是不能用头球取胜。
我有一个 Log
类,它有一个定义类 Log
的 header Log.h,然后 Log.cpp 实现了 Log 的方法
。我需要它在 Main.cpp 中可用,所以我将 Log.h 包含到 Main.cpp 中,但我收到了臭名昭著的“已定义”错误。
如果我从 Main.cpp 中取出 header ,我将无法使用该类。 如果我从 Log.cpp 中取出 header ,那么 Log(显然)未定义。
我赢不了!我该怎么办?
编辑 是的,我当然包括守卫。
#ifndef LOG_H_
#define LOG_H_
namespace vexal {
#define CCOL_RESET "^[[0m"
#define CCOL_RED "^[[31m"
class Log {
public:
Log();
virtual ~Log();
static void genInstance();
private:
static Log* _inst;
};
}
#endif /* LOG_H_ */
然后在 Log.cpp 和 Main.cpp 中的包含仅仅是 #include "Log.h"
。
最佳答案
你包括守卫了吗?
日志.h
#ifndef LOG_H
#define LOG_H
// your code here
#endif
关于C++: header 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9169516/