我在不同的 .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/