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.
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include<string>
usingnamespace 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
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();
}
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.