c++ - "Does not name a type"错误,但类指针已经有前向声明?

标签 c++ compiler-errors forward-declaration

我遇到这个编译器错误

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 直接或间接#includeDataAudit.h 的 header 。

关于c++ - "Does not name a type"错误,但类指针已经有前向声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18026877/

相关文章:

c++ - 由于头文件中的语法错误导致编译错误

c++ - 编译时检查连续函数调用

c# - 'CreateUserWizard1_CreatedUser' 没有重载匹配委托(delegate) 'System.EventHandler'

scala - 在 intellij 引用我在我使用过的类中实际没有使用的 scala 方法时出现错误

objective-c - 在 objective-c 中导入 header

c++ - 派生内部类的前向声明

c++ - -读取字符串字符时出错

c++ - 帮助在 std::map 中存储模板类的 intrusive_ptr

c++ - 将值从一个 vector 复制到另一个 vector (来自书籍)

c++ - 如何为内部类声明运算符<<