You wouldn't do this:
int value = 10;
int *p;
*p = value;
So why would you think this would work:
HubNode *hh = NULL;
HubNode **head;
*head = hh;
The indirection is the same, only the type has changed. And both snippets invoke undefined behavior. This code should do this:
HubNode *hh = NULL;
Tools::loadHubs(&hh);
cout << hh->name;
Further, your add function should be:
void HubNode::AddHub(const string& sname, const string& slocation, HubNode **head)
{
HubNode* newNode = new HubNode;
newNode->next = NULL;
newNode->name = sname;
newNode->location = slocation;
while (*head)
head = &(*head)->next;
*head = newNode;
}
It gets even more straight forward if you provide a proper constructor for HubNode
that takes the sname
and slocation
as construction parameters and initializes the node next
member to NULL. if you code that, the add becomes simply:
void HubNode::AddHub(const string& sname, const string& slocation, HubNode **head)
{
while (*head)
head = &(*head)->next;
*head = new HubNode(sname, slocation);
}