简体   繁体   English

c ++ templates:创建一个与现有对象相同的typename对象

[英]c++ templates: create an object of a typename same as of an existing object

I am using template classes in c++. 我在c ++中使用模板类。 I create an object of the class as below: 我创建了一个类的对象,如下所示:

Node<int> *rootNode = (Node<int> *) malloc(sizeof(Node<int>));

Now I insert few entries in the Node. 现在我在Node中插入一些条目。 After I see that the node is full, i want the code to create a new node with same typename as that of the root node and store the required data. 在我看到节点已满后,我希望代码创建一个与根节点具有相同类型名称的新节点并存储所需数据。 Below is my method for insertion: 以下是我的插入方法:

template <typename T>
RC Node<T>::Insert(void *key)
{
    if(space() > 0) { // check if current node has ample space    
             // add data in the current node
    }
    else
    {
        siblingNode = new Node<T>();
        if (this->Split(siblingNode, key)) {
            if (siblingNode != NULL) {
                siblingNode.display();
            }
        }
    }
}
}

I try to display the new node created using 我尝试显示使用创建的新节点

siblingNode.display() siblingNode.display()

method but it gives me compilation error 方法,但它给我编译错误

request for member ‘display’ in ‘siblingNode’, which is of non-class type ‘Node<int>*’

How to ensure that the siblingNode is of the same typename as that of the node from which the insert function is invoked ? 如何确保siblingNode的类型名与调用insert函数的节点的类型名相同?

siblingNode is a pointer, so you need to use the pointer member dereference operator: siblingNode是一个指针,因此您需要使用指针成员解除引用运算符:

siblingNode->display()

The error is telling you that the type you are dereferencing is a pointer, not that you need to have the same typename as Node<T> . 该错误告诉您,您要解除引用的类型是指针,而不是您需要与Node<T>具有相同的类型名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM