It's probably a good idea to get your program to work with non-templated classes first, and then convert it to a template.
Let's examine your program in the context of type int. This line in main():
calls the default constructor in the bag class:
Code:
class bag
{
public:
bag() {head = NULL;}
//default constructor
so b1.head = NULL. The next line in main() is:
which calls:
Code:
void bag::input()
{
int value;
cout << "Please enter a set: \n"; //set will have no spaces
cin >> value;
head_insert(head, value);
while (cin.peek() != '\n')
{
cin >> value;
head_insert(head, value);
}
}
So, lets say the user enters: 12345, and therefore value=12345. Then, head_insert() is called:
Code:
void head_insert(node* head, int value)
{
node* temp;
temp = new node(); //No parentheses
temp->set_data(value);
temp->set_link(head);
head = temp;
}
The function call looks like this: head_insert(NULL, 12345), and inside the function, you create a node; make a call to set_data() to set the node's data member equal to value, and then you call set_link() and send it b1.head, which is NULL. set_link() looks like this:
Code:
void set_link(node* link_field) {link = link_field;}
and it just sets the node pointer to NULL. The result is that any input from the user is put in a node
that points to nothing, and so you end up with a collection of nodes that all point to nothing. Then, you call print():
Code:
void bag::print()
{
node* temp = head;
cout << "{"; //output is like this {a,b,c,d}
if (head != NULL)
{
cout << temp->get_data();
temp = temp->get_link();
while (temp)
{
cout << "," << temp->get_data();
temp = temp->get_link();
}
cout << "}";
}
}
which outputs "{", and since b1.head equals NULL, the if statement is skipped and nothing is output after that. Note that even if you have only 1 node, the print() function won't display that node's data.