我正在尝试构建一个 BehaviourTree 数据结构。 “主”类是“BTNode”,叶子( Action 、命令或条件)是“BTLeaf”。因为我希望 BTLeaf 对我的实体执行一些操作,所以我将它设为一个模板类,它接受一个对象和一个成员函数指针。
btnode.h:
#ifndef BTNODE_H
#define BTNODE_H
#include <QLinkedList>
class BTNode
{
public:
BTNode();
BTNode(QLinkedList<BTNode *> &children);
~BTNode();
virtual bool execute() = 0;
protected:
QLinkedList<BTNode*> _children;
};
#endif // BTNODE_H
btleaf.h:
#ifndef BTLEAF_H
#define BTLEAF_H
#include "btnode.h"
template <class T> class BTLeaf : public BTNode
{
public:
BTLeaf(T* object, bool(T::*fpt)(void))
{ _object = object; _fpt=fpt; }
/* Does not work either:
BTLeaf(T* object, bool(T::*fpt)(void))
: BTNode()
{ _object = object; _fpt=fpt; }
*/
virtual bool execute()
{ return (_object->*_fpt)(); }
private:
bool (T::*_fpt)(); //member function pointer
T* _object;
};
#endif // BTLEAF_H
当我尝试构建解决方案(使用 Qt Creator)时,我得到:
spider.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall BTNode::BTNode(void)" (??0BTNode@@QAE@XZ) referenced in function "public: __thiscall BTLeaf<class Spider>::BTLeaf<class Spider>(class Spider *,bool (__thiscall Spider::*)(void))"
您可以看到我尝试的解决方案在我的代码中被注释掉了。如果我删除 public BTNode
部分并“手动”使用我的 btleaf,我会得到想要的结果。有什么想法吗?
编辑:
我以这种方式在我的 Spider
类中创建 BTLeaf 可能毫无值(value)(暂时,用于测试目的):
BTLeaf<Spider> test(this, &Spider::sayHello);
test.execute();
最佳答案
大概您声明的 BTNode 默认(无参数)构造函数未在任何地方定义(至少,您的链接器看到的任何地方都没有)。
关于c++ - 构建 C++ 模板类时未解析的外部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9898744/