Hi,
I wrote a template for a double linked list and i try to dynamically allocate an array where each element is a list from that template, but when i try to insert an element i get a compilation error and i don't understand the reason for getting this.
Here's the code:
template array definition:
Code:
List <gift> *buckets;
inserting the element:
Code:
gift * newGift;
...
...
buckets[pos].insertNode (*newGift);
element insertion code
Code:
template <class contain>
int List<contain>::insertNode (contain &element)
{
ListNode <contain> *temp, *newNode;
// if first element allocate first pointer.
if (!size) {
list = new ListNode <contain> (element);
lastNode = list;
return ++size;
}
//else traverse the list and insert element at the end.
temp = list;
while (temp -> getNext())
temp = temp -> getNext();
//if memory allocation error return 0
if (!(newNode = new ListNode <contain> (element))) return 0;
temp -> setNext (newNode);
lastNode = newNode;
return ++size; // else return the size of the list
}
and compiler error:
Code:
list.cpp: In member function ‘int List<contain>::insertNode(contain&) [with contain = gift]’:
santa.cpp:41: instantiated from here
list.cpp:48: error: could not convert ‘temp->ListNode<contain>::getNext [with contain = gift]()’ to ‘bool’
list.cpp:49: error: cannot convert ‘ListNode<gift>’ to ‘ListNode<gift>*’ in assignment
If any additional information is needed please let me know.
Thanks in advance.