生成C++模板类时未解析的外部
Unresolved external when building C++ Template Class
我正在尝试构建一个BehaviourTree数据结构。"main"类是"BTNode",叶子(动作、命令或条件)是"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可能毫无价值:
BTLeaf<Spider> test(this, &Spider::sayHello);
test.execute();
假设您声明的BTNode默认(无参数)构造函数没有在任何地方定义(至少,没有在链接器看到的任何地方定义)。
相关文章:
- 在函数内部的声明中初始化数组,并在外部使用它
- 使外部项目可用于find_package CMake
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- C++:来自外部文件的Trivia
- 从函数角度看ID到文件路径的内部与外部映射
- C++:将外部库链接到dll库
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 节俭并发:未解决的外部问题
- 如何在c++中从git建立外部库
- 未解析的外部符号_MsiLocateComponentW@12.
- 如何使用对C函数和类对象的外部调用来处理C++头文件
- 具有外部"c"和程序集的未定义函数
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 在C++中使用 gRPC 时未解析的外部符号
- 在类函数中初始化外部作用域变量
- 如果全局变量默认是外部变量,为什么要添加"extern"关键字?
- 调用外部函数,无法指定类型 C++/MVS
- 同时具有"外部"和"内联"说明符的变量
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta