c++ - 在不同的 header 问题中从基类派生类继承

标签 c++ inheritance

我认为我的代码中存在循环依赖性问题,但此时我不确定如何解决它。无论如何,我的 Behavior.h header 中有一个“行为”类:

#ifndef BEHAVIOR_H
#define BEHAVIOR_H

#include <list>
#include "DllEntry.h"

class Bot;

//Draws directly from BTSK, copyright alexjc

enum Status {
BH_INVALID,
BH_SUCCESS,
BH_FAILURE,
BH_RUNNING,
BH_ABORTED,
};


///Behavior: Base class for actions, conditions, and composites.
class Behavior {

protected:
Status m_eStatus;
Bot* m_pBot;

public:
Behavior(Bot& b) : m_eStatus(BH_INVALID), m_pBot(&b) {}
virtual ~Behavior() {}

virtual void OnInitialize() {}
virtual Status Update() = 0;
virtual void OnTerminate(Status) {}

Status Tick();

void Reset();
void Abort();

bool IsTerminated() const;
bool IsRunning() const;
Status GetStatus() const;
};

#endif

我的 HighLevelBehaviors.h header 中有一组 HighLevelBehaviors。这些 header 都继承自 Behavior 类:

#ifndef HIGHLEVELBEHAVIORS_H
#define HIGHLEVELBEHAVIORS_H

#include "Behavior.h"
#include "DllEntry.h"

//////////////////////////////////////////
//***Grab Healthpickup***/////////////////
//Selector: Finds health if health is low
//////////////////////////////////////////

class Bot;

///IsHealthLow: Condition node
//Return true if health is low
class IsHealthLow : public Behavior {

public:
IsHealthLow(Bot& b) : Behavior(b) {}
~IsHealthLow() {}

virtual void OnInitialize() {}
virtual Status Update();
virtual void OnTerminate(Status) {}
};


///FindClosestHealthPickup: Action node
//Return true if health a health pickup is found. Also
//sets a variable so that later Tasks can path to it.
// TODO Change to "FindClosestItem<"ITEM_CLASS">" so that we can it for multiple items.
// This will make it more modular.
class FindClosestHealthPickup : public Behavior {

public:
FindClosestHealthPickup(Bot& b) : Behavior(b) {}
~FindClosestHealthPickup() {}

virtual void OnInitialize() {}
virtual Status Update();
virtual void OnTerminate(Status) {}
};


///CreatePathToTarget: Action node
//Return true if path is successfully create
//Again, this can potentially be set up to reference different fields
//in the "Blackboard" data struct aka CreatePathToTarget(target="ENEMY")
//or CreatePathToTarget(target="HEALTH_PICKUP").
class CreatePathToHealthPickup : public Behavior {

public:
CreatePathToHealthPickup(Bot& b) : Behavior(b) {}
~CreatePathToHealthPickup() {}

virtual void OnInitialize();
virtual Status Update();
virtual void OnTerminate(Status) {}
};


///FollowPathToTarget: Action node
class FollowPathToHealthPickup : public Behavior {

public:
FollowPathToHealthPickup(Bot& b) : Behavior(b) {}
~FollowPathToHealthPickup() {}

virtual void OnInitialize();
virtual Status Update();
virtual void OnTerminate(Status);
};

#endif

我收到的错误是:

highlevelbehaviors.h(20): error C2504: 'Behavior' : base class undefined
highlevelbehaviors.h(27): error C2146: syntax error : missing ';' before identifier 'Update'
highlevelbehaviors.h(27): error C2433: 'IsHealthLow::Status' : 'virtual' not permitted on data declarations
highlevelbehaviors.h(27): warning C4183: 'Update': missing return type; assumed to be a member function returning 'int'
....

我想知道是否还有什么我可以做的,只是将两个 header 组合在一起,以便编译器可以看到我试图从中派生的基类。可能与范围有关吗?另外,我为我随意的评论惯例道歉,我仍在努力确定我最喜欢哪一个。感谢您的帮助!

还有一件事:如果需要,我可以包含这些文件的 .cpp 文件以及 Bot.h 文件,Behavior.h 和 HighLevelBehaviors.h 都包含(加粗是因为我我认为这可能很重要,但我不确定...)。

最佳答案

经过一些更改后,我的代码现在可以成功编译。这是我所做的: 根据@CaptainObvlious,我从 Behavior.h 和 HighLevelBehavior.h 中删除了#include "Bot.h",因为我已经在这两个文件中向前引用了 Bot 类。这意味着我需要将包含移动到实际的 .cpp 文件中,因为它们引用了类变量和函数。现在一切都可以编译。

虽然我会将此标记为答案,但我认为这显然意味着我需要重新评估我当前系统的设计,无论如何我都会这样做。感谢大家的帮助;我最终不仅解决了我的问题,还学习了一些关于#pragma 的好东西。我将使用更改更新代码。

关于c++ - 在不同的 header 问题中从基类派生类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23346460/

相关文章:

c++ - SDL/OpenGL 浮点异常

c++ - C++中的嵌套类访问控制

python - 在继承的情况下为 kwargs 输入 Mypy

java - 多态性中的内存管理是怎样的?

C++将类插入 map 容器

c++ - 在 C++11 中可移植地打印 std::uint64_t 变量的格式说明符

c++ - 移植到 Linux 时调用 wopen

c++ - 为什么强制内联函数会导致性能不佳?

ruby - 当 Ruby `Complex` 类除了 `Comparable` 之外没有任何关系运算符时,它的祖先怎么可能有 `==` 呢?

java - 如何 Autowiring 继承的Spring服务?