Printing out a list of objects from a custom list class.

I am having trouble figuring out how I print a list of objects from my custom list class. My List class is below. To insert the list inserts right before the current pointer. And the current pointer never moves unless you use the goToNext function. Now I have a patient class as well that has the basic private varible, first name, last name, ID and birth date. I included that code at the bottom of this post. The error I am getting is:
 
Error error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Patient' (or there is no acceptable conversion)	


My current item function just returns the current item function. I am not sure how to change it so it works for every type of List be it either a list of my patient class, string, int etc.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include<string>
using namespace std;

//typedef int ItemType;


//NodeType class, basically created the nodes in the List
template <typename ItemType>
class NodeType
{
public:
	ItemType data;
	NodeType * nextPtr;
};


//List Class
template <typename ItemType>
class List
{
public:
	List();
	~List();

	void makeEmpty();
	bool isEmpty();
	void goToStart();
	void goToNext();
	bool isAtEnd();
	ItemType currentItem();
	void insert(ItemType);
	void deleteCurrentItem();
	int currentPosition();

private:
	NodeType<ItemType>* head;
	NodeType<ItemType>* current;
	NodeType<ItemType>* previous;
	int position;
};


//List constructor
template <typename ItemType>
List<ItemType>::List()
{
	head = NULL;
	current = head;
	previous = head;
	position = 1;
}

//List Destructor
template <typename ItemType>
List<ItemType>::~List()
{
	makeEmpty();
}

//Insert before the current position
template <typename ItemType>
void List<ItemType>::insert(ItemType item)
{
	NodeType<ItemType> * value;
	value = new NodeType<ItemType>;
	value->data = item;
	if (position == 1)
	{
		current = value;
		current->nextPtr = head;
		head = current;
	}
	else//follow insutrctions
	{
		previous->nextPtr = value;
		value->nextPtr = current;
		current = value;
	}
}

//This functions checks whether the list is empty or not
template <typename ItemType>
bool List<ItemType>::isEmpty()
{
	return (head == NULL) ? true : false;
}

//returns the value of the Current Item
template <typename ItemType>
ItemType List<ItemType>::currentItem()
{
	if (head != NULL && current != NULL)
		return current->data;
}

//Put current pointer to the start and position is back at 1
template <typename ItemType>
void List<ItemType>::goToStart()
{
	current = head;
	previous = NULL;
	position = 1;
}

//if current pointer is NULL then the list is at the end and we return true
template <typename ItemType>
bool List<ItemType>::isAtEnd()
{
	return (current == NULL) ? true : false;
}

//delete the current item in the list
template <typename ItemType>
void List<ItemType>::deleteCurrentItem()
{
	if (head != NULL && current != NULL)
	{
		if (position == 1)
		{
			head = head->nextPtr;
			delete current;
			current = head;
		}
		else
		{
			previous->nextPtr = current->nextPtr;
			delete current;
			current = previous->nextPtr;
		}
	}
}

//Move to the next item in the list if it is not currently at the end
template <typename ItemType>
void List<ItemType>::goToNext()
{
	if (current != NULL)
	{
		previous = current;
		current = current->nextPtr;
		++position;
	}
}

template <typename ItemType>
int List<ItemType>::currentPosition()
{
	return position;
}

template <typename ItemType>
void List<ItemType>::makeEmpty()
{
	goToStart();
	while (current != NULL)
	{
		head = head->nextPtr;
		delete current;
		current = head;
	}
	delete head;
	delete previous;
}
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int main()
{
	List<Patient> patients;
	Patient bob;
	bob.setFirstName("Steve");
	patients.insert(bob);
        printList(patients);

	return 0;


}


template <typename ItemType>
void printList(List<ItemType> &list)
{
	int pos = list.currentPosition();
	list.goToStart();
	List::iterator put;
	//print out the list
	while (list.isAtEnd() != true)
	{
		cout << list.currentItem() << "]" << "->";
		list.goToNext();
	}

	//Move list back to the original position
	list.goToStart();
	for (int i = 1; i < pos; ++i)
		list.goToNext();
}


Patient Header:http://pastebin.com/qHk0xUAt
Patient class: http://pastebin.com/0Y8mUfMh
Last edited on
What you can do is overloading the stream operator<< for your Patient class:
1
2
3
4
5
6
7
8
9
10
class Patient
{
...
  friend std::ostream &operator<<(std::ostream &os, const Patient &p)
  {
    os << p.id; // if Patient has id
    return os;
  }
...
};
Got it to work. What I had to do was use
 
cout << list.currentItem().getFirstName();


But now what I am having trouble doing is modifying an object in the list. I have this code here:
1
2
3
4
5
6
7
	List<Patient> patients;
	Patient bob;
	bob.setFirstName("Bob");
	patients.insert(bob);
	cout << patients.currentItem().getFirstName() << endl;
	patients.currentItem().setFirstName("Steve");
	cout << patients.currentItem().getFirstName() << endl;

I expected the second cout to be steve but it's actually still bob. I added the patient header and cpp code to the original post in pastebin links. Any help would be appreciated.
The problem is that currentItem() returns a copy of the item (what would happen if current is actually NULL?).

That means you modify the copy not the original. Next time you call currentItem() you will get the original.

You should return a reference:
1
2
3
4
5
6
7
8
template <typename ItemType>
ItemType &List<ItemType>::currentItem() // Note: &
{
	if (current != NULL)
		return current->data;

  throw exception;
}
Last edited on
Topic archived. No new replies allowed.