0

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 ?

Tejas Patil
  • 6,149
  • 1
  • 23
  • 38
  • 1
    For starters, you shouldn't you be derefencing the `siblingNode` object with the `->` operator? – Aesthete Nov 10 '12 at 03:59
  • 2
    Don't use `malloc()` in C++. – iammilind Nov 10 '12 at 04:07
  • 1
    Adding to iammilind comment: ... and even less so mix `malloc` and `new` (some of your nodes are `malloc`-ed, some are `new`-ed, it is undefined behavior to release with a mechanism different from the one used to allocate, you are bound for some debugging time. Also note that depending on the definition of the `Node` template using `malloc` might actually lead to undefined behavior in itself (the constructor will not be called...) – David Rodríguez - dribeas Nov 10 '12 at 04:23
  • The code looks horrendous... – Kerrek SB Nov 10 '12 at 05:05
  • @iammilind thanks. I have not coded in C++ since 4 yrs and now i have to work with it. I would like to read more C++ internals (like the one you mentioned). Any book / website ? – Tejas Patil Nov 10 '12 at 06:56
  • @DavidRodríguez-dribeas thanks for the explanation. I have not coded in C++ since 4 yrs and now i have to work with it. I would like to read more C++ internals (like the one you mentioned). Any book / website ? – Tejas Patil Nov 10 '12 at 06:56
  • @KerrekSB i agree with you. I have not coded in C++ since 4 yrs and now i have to work with it. having hard time. this is off the question: any website/book about practices that u can suggest for improvement ? – Tejas Patil Nov 10 '12 at 06:59
  • 1
    @TejasP See here for book list, http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list I'm guessing you would benefit from this one, http://www.amazon.com/dp/0321334876/?tag=stackoverfl08-20 – john Nov 10 '12 at 07:28
  • 1
    @TejasP: Off the top of my head, I'd recommend sticking around SO for a few weeks and follow the C++ questions. Some of the answers contain a lot of "good practice", and there'll be comments about never-nevers. – Kerrek SB Nov 10 '12 at 14:08

1 Answers1

2

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

MSN
  • 53,214
  • 7
  • 75
  • 105