gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 22:32:50 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com