我在我的代码中遇到了周期性依赖问题,但是目前还不确定如何解决它。无论如何,我的Behavior.h标头中有一个“行为”类:
#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标头中还有一组HighLevelBehaviors。这些标头均继承自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'
....
我想知道是否还有其他事情可以做,只是将两个标头组合在一起,以便编译器可以看到我试图派生的基类。可能与范围有关吗?另外,对于我的随机评论惯例,我深表歉意,但我仍在尝试确定最喜欢哪一种。谢谢你的帮助!
还有一件事:如果需要,我可以包括.cpp文件以及Bot.h文件(Behaviour.h和HighLevelBehaviors.h都包括)(合并,因为我认为这可能很重要,但我不确定...)。
最佳答案
进行一些更改后,我的代码现在可以成功编译。这是我所做的:
按照@CaptainObvlious,我已经从Behavior.h和HighLevelBehavior.h中删除了#include“ Bot.h”,因为我已经在它们两个中都引用了Bot类。这意味着我需要将include移至实际的.cpp文件,因为它们引用了类变量和函数。现在一切都可以编译了。
虽然我将其标记为答案,但我认为它显然意味着我需要重新评估我当前系统的设计,无论如何我都会这样做。我感谢大家的帮助;我不仅解决了自己的问题,而且还学习了有关#pragma的一些好知识。我将使用更改来更新代码。
关于c++ - 不同头文件中基类的派生类继承,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23346460/