|
-
September 17th, 2008, 09:32 PM
#1
Link List Template errors?
In a class I took over a year ago I learned how to make a link list template. I saved that template and am now looking back it and trying to use it again for something else. However when I try to run the program with my template I get many errors, some of which are the same. They have to do with me using 'ostream' and 'out' and the compiler not liking that.
Here is the template I am attempting to use:
Code:
template <class T>
class listClass
{
public:
listClass(){head=tail=cur=NULL;}
~listClass()
{
free();
}
listClass(const listClass<T> &source)
{head=tail=cur=NULL; *this=source;}
const listClass<T> & operator=
(const listClass<T> &right);
bool isEmpty()const{return head==NULL;}
bool isFull()const{return false;}
bool isCurEnd()const{return cur==tail;}
void addItemToHead(const T& item);
void addItemToTail(const T& item);
const T& getFirst()const;
const T& getLast()const;
void removeFirst();
void removeLast();
void resetCur(){cur=head;}
void print(ostream &out)const;
void printCur(ostream &out);
private:
template <class T>
class nodeClass
{
public:
T data;
nodeClass<T> *next;
nodeClass()
{next=NULL;}
};
void free();
nodeClass<T> *head, *tail, *cur;
};
template <class T>
void listClass<T>::addItemToHead(const T& item)
{
nodeClass<T> *ptr=new nodeClass<T>;
ptr->data=item;
ptr->next=head;
head=ptr;
if(tail==NULL)
{
cur=tail=ptr;
}
}
template <class T>
void listClass<T>::addItemToTail(const T& item)
{
if(isEmpty())
addItemToHead(item);
else
{
nodeClass<T> *ptr=new nodeClass<T>;
ptr->data=item;
tail->next=ptr;
tail=ptr;
}
}
template <class T>
void listClass<T>::print(ostream &out)const
{
nodeClass<T> *ptr = head;
while(ptr!=NULL)
{
out << ptr->data << endl;
ptr=ptr->next;
}
}
template <class T>
const T& listClass<T>::getFirst()const
{
if(isEmpty())
{
cerr << "Empty list\n";
exit(41);
}
return head->data;
}
template <class T>
const T& listClass<T>::getLast()const
{
if(isEmpty())
{
cerr << "Empty list\n";
exit(41);
}
return tail->data;
}
template <class T>
void listClass<T>::removeFirst()
{
if(isEmpty())
{
cerr << "Empty list\n";
exit(41);
}
if(cur==head)
cur=head->next;
nodeClass<T> *ptr=head;
head=head->next;
delete ptr;
if(head==NULL)
tail=head;
}
template <class T>
void listClass<T>::removeLast()
{
if(isEmpty())
{
cerr << "Empty list\n";
exit(41);
}
if(head==tail)//one node
removeFirst();
else
{
nodeClass<T> *ptr=head;
while(ptr->next->next!=NULL)//while node after ptr is not tail
ptr=ptr->next;
if(cur==tail)
cur=ptr;
delete tail;
tail=ptr;
tail->next=NULL;
}
}
template <class T>
void listClass<T>::free()
{
while(!isEmpty())
removeFirst();
}
template <class T>
const listClass<T> & listClass<T>::operator=
(const listClass<T> &right)
{
if(this==&right)
return right;
free();
nodeClass<T> *ptr=right.head;
while(ptr!=NULL)
{
addItemToTail(ptr->data);
ptr=ptr->next;
}
return *this;
}
template <class T>
void listClass<T>::printCur(ostream &out)
{
if(cur==NULL)
{
cerr << "Invalid current pointer\n";
exit(42);
}
out << cur->data;
cur=cur->next;
}
Here's my main.cpp (doesn't really have anything in it):
Code:
#include <iostream>
#include <fstream>
#include "List.h"
using namespace std;
int main()
{
return 0;
}
I can't figure out why I'm getting errors since this template worked years ago and I haven't changed anything about it. Any help is appreciated.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|