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. Am I trying to do something that can't be done, or have I just made some mistake somewhere?

Answers


In Node(Node* parent, NodeObjectType* object);

Node is in reality Node<NodeObjectType, ChildNodeObjectType, ChildNode>>.

So for 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); 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.

Maybe you want some BaseNode as parent node instead.


Need Your Help

Passing a pointer by reference to class and using it

iphone objective-c pointers

I am trying to pass a pointer by reference to an object from class A to class B. In class B I want to assign this pointer to a ivar and read and write to it.

Grails - Weird behavior when using max param in domain list() method

grails gorm

I'm using spring-security-acl plugin and have User &amp; Role defined as a many-to-many relationship. I also have a UserRole domain class.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.