Be careful of your variable naming.
Code:
$ g++ -Wall -Wshadow -c foo.cpp
foo.cpp: In member function ‘void StringList::insertBack(std::__cxx11::string)’:
foo.cpp:54:19: warning: declaration of ‘temp’ shadows a previous local [-Wshadow]
ListNode *temp = head;
^
foo.cpp:45:15: note: shadowed declaration is here
ListNode *temp = new ListNode;
^
You have two variables called temp in two nested scopes.
Code:
void StringList::insertBack(std::string r)
{
ListNode *newNode = new ListNode;
newNode->value = r;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
}
else
{
ListNode *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode; // actually append it
}
}