gpt4 book ai didi

c++ - 类中枚举的前向声明?

转载 作者:太空狗 更新时间:2023-10-29 23:12:06 27 4
gpt4 key购买 nike

我有两个抽象类,它们彼此拥有另一个的指针。我需要在其中一个中使用另一个类的枚举,例如示例。AFoo 持有 ABar,ABar 需要一个指向 AFoo 的指针来更新一些数据,还需要一个使用 AFoo 枚举的成员函数。

我记得曾经遇到过这个问题,但不是枚举,我最终做了内联声明。

我可以做一个嵌套类,但有没有其他方法可以避免这种情况?

AFoo.hpp :

#include ...

class AFoo;
enum AFoo::poo; --> not possible

#include "ABar.hpp"

class AFoo {
public:
...
virtual void func() = O;
enum poo {
H,
I,
...
}
protected:
ABar *bar_;
};

ABar.hpp

#include ...

class Abar;

#include "AFoo.hpp"

class ABar {
public:
...
virtual AFoo::poo doing_some_stuff() = 0; --> Here is my problem (if i replace the return type with basic type i have no compilation problem)
protected:
AFoo *foo_;
};

最佳答案

在AFoo.hpp中,不包含ABar.hpp,只前向声明类:

class ABar;

此外,在 ABar.hpp 中,包含 AFoo.hpp 并且不要转发 ABarAFoo

关于c++ - 类中枚举的前向声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48254335/

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