【发布时间】:2016-06-17 02:49:39
【问题描述】:
noobie程序员在这里~
我正在尝试从我的演讲幻灯片中复制一些代码,但是,幻灯片没有显示头文件设置。两个类使用彼此(Agent 和 IBehaviour),所以在娱乐时我遇到了一个循环包含。我尝试使用前向声明解决方案(在 IBehaviour 中)来解决我的问题,但是现在我遇到了一个未解决的外部错误......'
我想我可以看到我的问题 ~ 我有一个函数接收指向 IBehaviour.h 中不完整类型的指针,但是我不知道如何解决这个问题。
非常感谢任何帮助。以下代码已压缩为 h 文件以供阅读。
Agent.h
#pragma once
#include "IBehaviour.h"
#include <list>
class Agent
{
public:
Agent();
~Agent();
void Update();
protected:
std::list<IBehaviour*> m_behaviours;
};
Agent::Agent()
{
}
Agent::~Agent()
{
}
void Agent::Update()
{
for (auto iter = m_behaviours.begin(); iter != m_behaviours.end(); iter++)
(*iter)->Update(this);
}
IBehaviour.h
class Agent;
class IBehaviour
{
public:
IBehaviour();
~IBehaviour();
virtual void Update(Agent* pAgent);
};
IBehaviour::IBehaviour()
{
}
IBehaviour::~IBehaviour()
{
}
void IBehaviour::Update(Agent * pAgent)
{
}
SeekB
#pragma once
#include "IBehaviour.h"
class SeekB : public IBehaviour
{
public:
SeekB();
~SeekB();
void Update(Agent *pAgent) override;
};
SeekB::SeekB()
{
}
SeekB::~SeekB()
{
}
void SeekB::Update(Agent * pAgent)
{
// Apply seek force to pAgent
}
【问题讨论】:
标签: c++ header forward-declaration