我遇到这个编译器错误
error: 'RawLog' does not name a type
相关代码如下:
//DataAudit.h
#ifndef DATAAUDIT_H
#define DATAAUDIT_H
class RawLog;
class DataAudit
{
...
private:
RawLog* _createNewRawLog(); // This is the line indicated with the error
};
#endif // DATAAUDIT_H
通常前向声明可以解决这种错误。 This answer 表示循环 header 包含可能会导致此问题。但是 #ifndef
和 #define
语句的使用不会阻止循环 header 包含吗?
我可能会看到此错误还有其他原因吗?
我可以使用哪些方法来进一步推断此错误的性质?
更新:这很奇怪。我有一个 Globals.h
文件,如果我在 Globals.h
中定义一个新的 enum
,就会出现错误。然后,如果我注释掉 enum
,错误就会消失。这让我想到循环依赖已经存在了一段时间,并且添加 enum
以某种方式重新排序编译单元,从而暴露之前不存在的依赖?
最佳答案
#ifndef
header guard 不会阻止循环依赖。它只是防止在单个文件中多次包含相同的 header 。
对我来说看起来像是循环依赖。这意味着您可以在 DataAudit.h 中#include
直接或间接#include
DataAudit.h 的 header 。
关于c++ - "Does not name a type"错误,但类指针已经有前向声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18026877/