简体   繁体   中英

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

I am using template classes in 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. 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()

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 is a pointer, so you need to use the pointer member dereference operator:

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> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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