I'm trying to write a function that accepts a linked list as its argument. I then create a pointer of the linked list class and set it equal to the head of the lined list.
1 2 3 4 5 6 7 8 9 10 11
WeatherStats greatestMonth(LinkList<WeatherStats> list)
{
LinkList<WeatherStats> *headptr = &list;
double currentGreatest = 0; //local variable holds the largest node value
while (headptr != NULL)
{
//some function that sets currentGreatest if this node's value is higher
headptr = headptr->next;
}
on the last line I am attempting to incriment the headptr to the next node by setting it equal to the next pointer.
I keep getting an error :
Error 1 error C2039: 'next' : is not a member of 'LinkList<T>'
My Node looks like this:
1 2 3 4 5 6 7 8 9 10
template <class T>
class ListNode
{
private:
T value1;
ListNode<T> *next;
public:
//***Constructors***
ListNode(T);
};
template <class T>
class LinkList
{
private:
ListNode<T> *head;
public:
//***Constructor***
LinkList();
//***Destructor***
~LinkList();
//***LinkList Operations***
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList() const;
T getHead();
T searchList(T);
};
The error doesn't make sense to me. I know that the linklist has a member next in the list node but I don't know how to access it form main. I need to do this in order to increment through the list in my greatesMonth function. I think to move the headptr through the list I have to set the headptr equal to the pointer next. Am I wrong? If so I would sure like some help and maybe some explanation in how this is done.
You are trying to work with a pointer to the wrong type. You have a pointer to a list (which has no next member, as per the error message). What you want is a pointer to a ListNode (which does have a next member).
1 2 3 4 5 6 7 8 9 10 11 12 13
WeatherStats greatestMonth(LinkList<WeatherStats> list)
{
// LinkList<WeatherStats> *headptr = &list;// wrong type
ListNode* headptr = list.head;
double currentGreatest = 0; //local variable holds the largest node value
while (headptr != NULL)
{
//some function that sets currentGreatest if this node's value is higher
headptr = headptr->next;// friend of ListNode class too? next is private there.
}
}
greatestMonth() will need to be a friend function of the LinkList class because list.head refers to a private member. There is a public getHead() function, but it appears to return the value stored there, not a pointer to the head ListNode.
Looks like it needs to be friends with ListNode too, due to next being private.
For that matter, how did you code the appendNode(), insertNode(), deleteNode(), displayList()'s without access to any ListNodes next member? There is no public getNext() in ListNode.
Thanks for the help! I had the appendNode(), insertNode, etc already in the LinkList class header file. As for how next is accesed, here is my appendNode code, it works though I agree...how?:
template <class T>
void LinkList<T>::appendNode(T val1)
{
ListNode<T> *newNode;
ListNode<T> *nodePtr;
newNode = new ListNode<T>(val1);
if (!head)
{
head = newNode;
}
else
{
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
After much turmoil and anguish over that very issue (we were not allowed to alter the template) last night I decided to sent my teacher an email and find out what was going on. It turns out that we aren't supposed to get the data for determining greatest amount from the list. Apparently we are supposed to store those numbers in an array as they are being entered into the program by the user but before they are added to a link node and appended to the list. That seems a bit redundant but hey what do I know...I'm a noob!