Trying to implement a function in my linkedlist class that will return total amount of data stored within the list. i.e. linkedlist of 5, 10, 20 total would return 35.
My node class contains the getNextPtr and getData methods.
I implement a new Node and call it currentPtr and make it point to headPtr.
On compile i get: “36-request for member getNextPtr in currentPtr which is of non class type”
It’s the same for 38 and 40, with 38 being the getData in currentPtr.
Not quite sure what I’m missing…
int LinkedList::getTotal()
{
int total = 0;
Node *currentPtr = headPtr;
(36) while(currentPtr.getNextPtr() != NULL)
{
(38) total += currentPtr.getData();
(40) currentPtr = currentPtr.getNextPtr();
}
return total;
}
The idea is to travel through the linked list until it reaches tailptr which will point to null, adding whatever data it encounter in to total.
Hope that makes some sense, thanks in advance 🙂
If you want to dereference a pointer you need to use
->, not.e.g. your code becomes:
I also find it more natural to write
while(currentPtr)(i.e. ‘while I have a valid pointer’) instead of explicitly testing forNULL.