c++ - 具有前向声明的相互依赖的 .h 文件

标签 c++ header-files forward-declaration

我在不同的 .h 文件中有 3 个类(A、B 和 C)。我怎样才能移动 #include 和前向声明以使其编译。

目前我在 A.h 中使用了前向声明,并认为它可以从那里开始工作。相反,C.h 抛出许多编译器错误 'class A' is inaccessible with in this context

// A.h
#pragma once

...

class B;

class A {
  private:
    B *parent_;
};
// B.h
#pragma once

...

#include <A.h>

class B : A {
  public:
    virtual void func(A *arg);
};
// C.h
#pragma once

...

#include <A.h>
#include <B.h>

class C : B {
  public:
    virtual void func(A *arg);

  private:
    A *left_child;
    A *right_child;
};

最佳答案

名称 A 通过B 私有(private)。您可以在 B 中更改为 public 或 protected 继承,或者在 C

中使用(完全限定名称)::A >

关于c++ - 具有前向声明的相互依赖的 .h 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815598/

相关文章:

objective-c - Objective-C中使用C函数多次导入头文件

c - 前向声明错误我无法理解

C++ 头文件 (.h) 或 C 中的 .h 文件包含滤波器系数

c++ - 使用不完整类型参数化的模板的外部模板

c - 前向声明如何运作?

c++ - Visual Studio 不断嵌入错误的资源文件

c++ - 运行良好,但在完成之前崩溃

c++ - 我想在 C++ 中实现一种采用整数值加上无穷大符号的数据类型。

c++ - 提升 python : how to call a C++ virtual function

C++:带有删除器的类的前向声明,用于可重复的唯一指针