[英]No matching constructor for initialization of constructor of templated class
I'm trying to create a template based tree where the nodes are specialized such that the methods accessing the element of the node and the child nodes return the child node type. 我正在尝试创建一个基于模板的树,其中的节点经过特殊处理,以便访问节点和子节点元素的方法返回子节点类型。 Below is the abridged version of the code.
下面是该代码的简化版本。
template<class NodeObjectType, class ChildNodeObjectType, class ChildNode>
class Node {
public:
Node(Node* parent, NodeObjectType* object);
ChildNode* addAsChild(ChildNodeObjectType* object);
private:
NodeObjectType* nodeObject;
std::vector<ChildNode*> children;
};
typedef Node<B, void, void> BNode;
typedef Node<A, B, BNode> ANode;
typedef Node<void, A, ANode> RootNode;
/* constructor */
template<class NodeObjectType, class ChildNodeObjectType, class ChildNode>
ChildNode* Node<NodeObjectType, ChildNodeObjectType, ChildNode>::
addAsChild(ChildNodeObjectType* object)
{
ChildNode* child = new ChildNode(this, object);
children.push_back(child);
return child;
}
The error is in the new ChildNode() call in addAsChild. 错误发生在addAsChild中的new ChildNode()调用中。 Am I trying to do something that can't be done, or have I just made some mistake somewhere?
我是在尝试做一些无法完成的事情,还是只是在某个地方犯了一些错误?
In Node(Node* parent, NodeObjectType* object);
在
Node(Node* parent, NodeObjectType* object);
Node
is in reality Node<NodeObjectType, ChildNodeObjectType, ChildNode>>
. 实际上,
Node
是Node<NodeObjectType, ChildNodeObjectType, ChildNode>>
。
So for ANode
: ANode(ANode* parent, A*);
因此对于
ANode
: ANode(ANode* parent, A*);
So when you do something like: 因此,当您执行以下操作时:
A a;
RootNode root(nullptr, nullptr);
ANode anode(nullptr, &a);
root.addAsChild(&a);
you will call ChildNode* child = new ChildNode(this, object);
您将调用
ChildNode* child = new ChildNode(this, object);
or with some replacement: 或进行一些替换:
ANode child = new ANode(root, &a);
// error: no matching function for call to
// 'Node<A, B, Node<B, void, void> >::Node(Node<void, A, Node<A, B, Node<B, void, void> > >* const, A*&)'
As RootNode
is a different type as ANode
(template parameters differ), there is a compilation error. 由于
RootNode
是与ANode
不同的类型(模板参数不同),因此存在编译错误。
Maybe you want some BaseNode
as parent node instead. 也许您想要一些
BaseNode
作为父节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.