在不同的头问题中从基类派生类继承

时间:2014-04-28 16:32:57

标签: c++ inheritance

我在我的代码中遇到了我认为的循环依赖问题,但我不确定如何在此时对其进行故障排除。无论如何,我的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文件,其中都是Behavior.h和HighLevelBehaviors.h包含(粗体,因为我认为这可能很重要,但我不确定......)。

1 个答案:

答案 0 :(得分:1)

经过一些更改后,我的代码现在已成功编译。这就是我的所作所为: 根据@CaptainObvlious,我删除了#include&#34; Bot.h&#34;来自Behavior.h和HighLevelBehavior.h,因为我已经在前面引用了两者中的Bot类。这意味着我需要将include移动到实际的.cpp文件,因为它们引用了类变量和函数。现在一切都在编译。

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