For my class we have to make our own generic template list structure. I made it so that it compiles fine with integers but am having an issue with strings, when it compiles I get the following error:
1 2 3 4
1>c:\users\jeremy\desktop\school related\cse 330 data structures\list\list\main.cpp(118) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1> could be 'built-in C++ operator==(const char [3], const char [3])'
1> while trying to match the argument list '(std::string, const char [3])'
1>Build log was saved at "file://c:\Users\Jeremy\Desktop\School Related\CSE 330 Data Structures\List\List\Debug\BuildLog.htm"
This is my list class, which is broken into three classes.
#ifndef proj_List_H
#define proj_List_H
template <class T>
class ListLink;
template <class T>
class ListIterator;
template <class T>
class List;
template <class T>
class ListLink
{
public:
ListLink(T x);
private:
T x;
ListLink<T> * prev;
ListLink<T> * next;
friendclass ListIterator<T>;
friendclass List<T>;
};
template <class T>
class ListIterator
{
public:
typedef ListIterator<T> iterator;
//constructor
ListIterator(ListLink<T> * link);
//operators
T & operator *();
voidoperator=(iterator & right);
booloperator==(iterator & right);
booloperator!=(const iterator & right) const;
iterator operator++();
iterator operator--();
private:
ListLink<T> * link;
friendclass ListLink<T>;
friendclass List<T>;
};
template <class T>
class List
{
public:
typedef ListIterator<T> iterator;
//constructors
List();
~List();
//member functions
unsignedint size() const;
void push_back(const T x);
T & front();
T & back();
void push_front(const T & x);
void pop_front();
void pop_back();
iterator begin();
iterator end();
void erase(iterator position);
void clear();
private:
unsignedint my_size;
ListLink<T> * firstLink;
ListLink<T> * lastLink;
friendclass ListLink<T>;
friendclass ListIterator<T>;
};
template <class T>
ListLink<T>::ListLink(T x) : x(x), prev(0), next(0)
{
}
//iterator class
template <class T>
ListIterator<T>::ListIterator(ListLink<T> * link): link(link) //constructor that takes an integer for the current link
{
}
template <class T>
T & ListIterator<T>::operator *()
{
return link->x;
}
template <class T>
void ListIterator<T>::operator=(iterator & right) //operator that assigns link to the right.link
{
link = right.link;
}
template <class T>
bool ListIterator<T>::operator==(iterator & right) //returns true if the the right of the argument is equal to the current link
{
return link == right.link;
}
template <class T>
bool ListIterator<T>::operator!=(const iterator & right) const //returns true if the the right of the argument is not equal to the current link
{
return link != right.link;
}
template <class T>
typename ListIterator<T>::iterator ListIterator<T>::operator++()//returns the links next link
{
link = link->next;
return *this;
}
template <class T>
typename ListIterator<T>::iterator ListIterator<T>::operator--()//returns the links previous link
{
link = link->prev;
return *this;
}
//List class
template <class T>
List<T>::List() : my_size(0), firstLink(0), lastLink(0)
{
}
template <class T>
List<T>::~List() //destructor deletes all the links
{
}
template <class T>
unsignedint List<T>::size() const
{
return my_size;
}
template <class T>
void List<T>::push_back(const T x)//accepts a const integer and puts it at the back
{
ListLink<T> * newLink = new ListLink<T>(x);
ListLink<T> * tempLink = lastLink;
if (my_size == 0) //if the list is empty
push_front(x);
else
{
lastLink = newLink;
newLink->prev = tempLink;
newLink->next = 0;
++my_size; //increase value of my_size by 1, doesn't do this if size ==0 because push_front will
}
}
template <class T>
T & List<T>::front() //returns the a reference to the firt element in the list
{
return firstLink->x;
}
template <class T>
T & List<T>::back() //returns the a reference to the last element in the list
{
return lastLink->x;
}
template <class T>
void List<T>::push_front(const T & x)//accepts a const integer and puts it at the front
{
ListLink<T> * newLink = new ListLink<T>(x);
if (my_size == 0) //if the list is still empty
firstLink = lastLink = newLink;
else
{
firstLink->prev = newLink;
newLink->next = firstLink;
firstLink = newLink;
}
++my_size; //increments value of my_size by 1
}
template <class T>
void List<T>::pop_front()//deletes the link at the at the beginning
{
ListLink<T> * tempLink = firstLink; //saves the first link temporarily
firstLink = firstLink->next; //sets the firstLink to the next link
if (firstLink != 0) //as long as firstLink isn't 0 it then sets firstLink's previous link to 0
firstLink->prev = 0;
else//or it sets the value of last link to 0 and deletes the temp value
{
lastLink = 0;
delete tempLink;
}
--my_size; //reduces my_size by 1;
}
template <class T>
void List<T>::pop_back()//deletes the link at the end of the list
{
ListLink<T> * tempLink = lastLink; //saves the last link temporarily
lastLink = lastLink->prev; //sets the lastLink to the prev link
if (lastLink != 0) //if the last link isn't equal to 0 it deletes the templink
delete tempLink;
else//or it sets the value of last link to 0
{
lastLink = 0;
}
--my_size; //reduces my_size by 1;
}
template <class T>
typename ListIterator<T> List<T>::begin()//returns the iterator of the firstLink
{
return iterator(firstLink);
}
template <class T>
typename ListIterator<T> List<T>::end() //returns the iterator of lastLink
{
return iterator(lastLink);
}
template <class T>
void List<T>::erase(iterator position) //function that erases the value at iterator position
{
if(position.link == firstLink)
pop_front();
elseif (position.link == lastLink)
pop_back();
else
{
position.link->next->prev = position.link->prev;
position.link->prev->next = position.link->next;
delete position.link;
--my_size; //reduces my_size by 1;
}
}
template <class T>
void List<T>::clear()
{
while (my_size > 0) //runs until my_size == 0
{
ListLink<T> * tempLink = lastLink; //creates tempLink and sets it equal to the last link
lastLink = lastLink->prev; //sets lastLink equal to the preceding link
delete tempLink;//deletes temp link
my_size--;//reduces my_size by 1;
}
}
#endif