C++ C++;模板与多态性
我正在研究决策树结构,因为我将对不同的代理使用类似的决策结构,所以我决定使用模板来构建基本功能 目前我有一个名为C++ C++;模板与多态性,c++,templates,inheritance,polymorphism,C++,Templates,Inheritance,Polymorphism,我正在研究决策树结构,因为我将对不同的代理使用类似的决策结构,所以我决定使用模板来构建基本功能 目前我有一个名为BC_IsEnemyCloseDecision的类,它是从模板类继承的。链是BC\u IsEnemyCloseDecision->DT\u BoolDecision->DT\u Decision->DT\u Node dtu节点class: template <class entity_type> class DT_Node { public: DT_Node()
BC_IsEnemyCloseDecision
的类,它是从模板类继承的。链是BC\u IsEnemyCloseDecision->DT\u BoolDecision->DT\u Decision->DT\u Node
dtu节点
class:
template <class entity_type>
class DT_Node
{
public:
DT_Node() {}
virtual ~DT_Node()
{
mNodes.clear();
}
virtual DT_Node* decide(entity_type* pAgentPtr) = 0;
virtual void addChild(std::unique_ptr<DT_Node>&& pNewChild)
{
mNodes.emplace_back( std::move(pNewChild) );
}
protected:
std::vector< std::unique_ptr<DT_Node> > mNodes;
};
template <class entity_type>
class DT_BoolDecision : public DT_Decision<entity_type>
{
public:
enum eNODE_TYPE{ eNT_TRUE_NODE = 0, eNT_FALSE_NODE};
DT_BoolDecision()
: DT_Decision<entity_type>(),
BRANCH_NUMBER(2)
{
}
virtual ~DT_BoolDecision()
{
}
void addChild(std::unique_ptr< DT_Node<entity_type> >&& pNewChild)
{
this->mNodes.emplace_back( std::move(pNewChild) );
}
void addChild(const eNODE_TYPE pNodeType, std::unique_ptr< DT_Node<entity_type> >&& pNewChild)
{
if(this->mNodes.size() <BRANCH_NUMBER)
{
this->mNodes.emplace( this->mNodes.begin() + (int)pNodeType, std::move(pNewChild) );
}
}
protected:
virtual bool performTest(entity_type* pAgentPtr) = 0;
DT_Node<entity_type>* getBranch(entity_type* pAgentPtr)
{
if( performTest(pAgentPtr) )
{
return this->mNodes[eNODE_TYPE::eNT_TRUE_NODE].get();
}
return this->mNodes[eNODE_TYPE::eNT_FALSE_NODE].get();
}
const int BRANCH_NUMBER;
};
// int for a simple test
class BC_IsEnemyCloseDecision : public DT_BoolDecision<int>
{
public:
BC_IsEnemyCloseDecision();
virtual ~BC_IsEnemyCloseDecision();
protected:
bool performTest(int* pAgentPtr);
};
template <class entity_type>
class DecisionTree
{
public:
DecisionTree(entity_type* pAgentPtr)
{
mAgentPtr = pAgentPtr;
}
DecisionTree(entity_type* pAgentPtr, std::unique_ptr< DT_Node<entity_type> >&& pRoot)
{
mAgentPtr = pAgentPtr;
mRoot = std::move(pRoot);
}
~DecisionTree()
{
}
void addRoot(std::unique_ptr< DT_Node<entity_type> >&& pRoot)
{
mRoot = std::move(pRoot);
}
void decide()
{
DT_Action<entity_type>* resultantAction = (DT_Action<entity_type>*)(mRoot->decide(mAgentPtr));
if(resultantAction != nullptr)
{
resultantAction->performAction(mAgentPtr);
}
else
{
std::cout<<"ERROR! DT! FACED WITH A NULL DT_ACTION";
}
}
private:
std::unique_ptr<DT_Node<entity_type>> mRoot;
entity_type* mAgentPtr;
};
BC\u IsEnemyCloseDecision
class:
template <class entity_type>
class DT_Node
{
public:
DT_Node() {}
virtual ~DT_Node()
{
mNodes.clear();
}
virtual DT_Node* decide(entity_type* pAgentPtr) = 0;
virtual void addChild(std::unique_ptr<DT_Node>&& pNewChild)
{
mNodes.emplace_back( std::move(pNewChild) );
}
protected:
std::vector< std::unique_ptr<DT_Node> > mNodes;
};
template <class entity_type>
class DT_BoolDecision : public DT_Decision<entity_type>
{
public:
enum eNODE_TYPE{ eNT_TRUE_NODE = 0, eNT_FALSE_NODE};
DT_BoolDecision()
: DT_Decision<entity_type>(),
BRANCH_NUMBER(2)
{
}
virtual ~DT_BoolDecision()
{
}
void addChild(std::unique_ptr< DT_Node<entity_type> >&& pNewChild)
{
this->mNodes.emplace_back( std::move(pNewChild) );
}
void addChild(const eNODE_TYPE pNodeType, std::unique_ptr< DT_Node<entity_type> >&& pNewChild)
{
if(this->mNodes.size() <BRANCH_NUMBER)
{
this->mNodes.emplace( this->mNodes.begin() + (int)pNodeType, std::move(pNewChild) );
}
}
protected:
virtual bool performTest(entity_type* pAgentPtr) = 0;
DT_Node<entity_type>* getBranch(entity_type* pAgentPtr)
{
if( performTest(pAgentPtr) )
{
return this->mNodes[eNODE_TYPE::eNT_TRUE_NODE].get();
}
return this->mNodes[eNODE_TYPE::eNT_FALSE_NODE].get();
}
const int BRANCH_NUMBER;
};
// int for a simple test
class BC_IsEnemyCloseDecision : public DT_BoolDecision<int>
{
public:
BC_IsEnemyCloseDecision();
virtual ~BC_IsEnemyCloseDecision();
protected:
bool performTest(int* pAgentPtr);
};
template <class entity_type>
class DecisionTree
{
public:
DecisionTree(entity_type* pAgentPtr)
{
mAgentPtr = pAgentPtr;
}
DecisionTree(entity_type* pAgentPtr, std::unique_ptr< DT_Node<entity_type> >&& pRoot)
{
mAgentPtr = pAgentPtr;
mRoot = std::move(pRoot);
}
~DecisionTree()
{
}
void addRoot(std::unique_ptr< DT_Node<entity_type> >&& pRoot)
{
mRoot = std::move(pRoot);
}
void decide()
{
DT_Action<entity_type>* resultantAction = (DT_Action<entity_type>*)(mRoot->decide(mAgentPtr));
if(resultantAction != nullptr)
{
resultantAction->performAction(mAgentPtr);
}
else
{
std::cout<<"ERROR! DT! FACED WITH A NULL DT_ACTION";
}
}
private:
std::unique_ptr<DT_Node<entity_type>> mRoot;
entity_type* mAgentPtr;
};
完成编码后,我决定用一个简单的类型测试它(我选择了int
),并执行以下操作:
int test= 6;
std::unique_ptr<BC_IsEnemyCloseDecision> testRoot = std::make_unique<BC_IsEnemyCloseDecision>();
std::unique_ptr<BC_IsEnemyCloseDecision> testNode = std::make_unique<BC_IsEnemyCloseDecision>();
testRoot->addChild(testNode); // Error: No matching member function for call to 'addChild'
testRoot->addChild(DT_BoolDecision<int>::eNODE_TYPE::eNT_TRUE_NODE, testNode); // Error: No matching member function for call to 'addChild'
DecisionTree<int> dt(&test);
dt.addRoot(testRoot); // Error: No viable conversion from 'unique_ptr<BC_IsEnemyCloseDecision>' to 'unique_ptr<DT_Node<int>>
dt.decide();
int测试=6;
std::unique_ptr testRoot=std::make_unique();
std::unique_ptr testNode=std::make_unique();
testRoot->addChild(testNode);//错误:调用“addChild”时没有匹配的成员函数
testRoot->addChild(DT_BoolDecision::eNODE_TYPE::eNT_TRUE_NODE,testNode);//错误:调用“addChild”时没有匹配的成员函数
决策树dt(和测试);
dt.addRoot(testRoot);//错误:无法从“唯一”转换为“唯一”
dt.decision();
并收到没有匹配的成员函数用于调用“addChild”
和没有从“unique\u ptr”到“unique\u ptr”的可行转换
我找不到任何与我的问题相关的有用信息,因此我没有解决问题的见解。你的错误归结为
struct Receiver
{
void method(std::unique_ptr<int> ptr) {}
};
int main()
{
Receiver receiver;
std::unique_ptr<int> ptr = std::make_unique<int>(10);
receiver.method(ptr); // Error: No matching member function for call to 'method'
}
您认为在这里粘贴3页代码以查找错误是一个好主意吗?为什么你不缩小它,这样任何人都可以重现这个问题呢;请自己花点时间把范围缩小。阅读a是什么,类DT_动作不存在。。所以什么都不能编译<代码>testRoot->addChild(testNode)
virtualvoidaddchild(std::unique\u ptr&&pNewChild)
你想不想?@ciyo好吧,如果你的函数需要一个。另外,如果添加std::move
,您的代码可能会编译,但我预计它会很快崩溃。你知道当你移动constructunique\u ptr
然后尝试访问原始文件时会发生什么吗?这里有一个