Hello all,
I am having a bit of trouble overloading the [] operator on a template based LinkedList class. Could you all tell me what I am doing wrong? Thank you so much. Basically it is to provide my template based linkedlist class to act as a normal array.
Here is is the code I am having trouble with, and I will post the full code below it:
TroubleCode:
template <class T>
T &LinkedList<T>::operator[](int posistion){
ListNode *ptrNode;
int countItems = this->count();
int count = 0;
if(posistion < 0 || posistion >= countItems){
int arrayCount = this->count();
throw InvalidPosistion(posistion, arrayCount);
}
else
ptrNode = head;
for(ptrNode = head; count != countItems; count++){
ptrNode = ptrNode->next;
}
return ptrNode->value;
}
rest of code:
#pragma once
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class T>
class DuplicateException{};
template < class T >
class LinkedList{
private:
class ListNode{
public:
T value;
ListNode *next;
ListNode (T v, ListNode *n = NULL) : value(v), next(n) { }
};
ListNode *head;
public:
LinkedList(ListNode *ptr = NULL){head = ptr;}
void push_back(T object);
void insert(T object);
void erase(T object);
void displayList();
int count();
T &operator[](const int posistion);
LinkedList<T> &operator=(const LinkedList<T> &rhs);
class InvalidPosistion{
public:
string message;
InvalidPosistion(int posistion, int maxCount){
std::ostringstream message1;
message1 << "Invalid array index of " << posistion << ". Valid array index range is 0 - " << maxCount << ".";
message = message1.str();
}
};
};
template <class T>
T &LinkedList<T>::operator[](int posistion){
ListNode *ptrNode;
int countItems = this->count();
int count = 0;
if(posistion < 0 || posistion >= countItems){
int arrayCount = this->count();
throw InvalidPosistion(posistion, arrayCount);
}
else
ptrNode = head;
for(ptrNode = head; count != countItems; count++){
ptrNode = ptrNode->next;
}
return ptrNode->value;
}
template <class T>
LinkedList<T> &LinkedList<T>::operator=(const LinkedList<T> &rhs){
ListNode *ptrNode;
if(!head){
ptrNode = NULL;
rhs.insert(ptrNode);
}
else{
ptrNode = head;
while(ptrNode->value)
rhs.insert(ptrNode);
}
}
template <class T>
void LinkedList<T>::push_back(T object){
ListNode *newNode = new ListNode(object), *nodePtr;
if(!head)
head = newNode;
else{
nodePtr = head;
while(nodePtr->next != NULL){
if(nodePtr->value == newNode->value){
throw DuplicateException<T>();
}
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
}
}
template <class T>
void LinkedList<T>::displayList(){
ListNode *nodePtr;
for(nodePtr = head; nodePtr; nodePtr = nodePtr->next)
cout << nodePtr->value << endl;
}
template <class T>
void LinkedList<T>::insert(T object){
ListNode *newNode = new ListNode(object);
ListNode *nodePtr, *previousNode = NULL;
if(!head)
head = newNode;
else{
for(nodePtr = head; nodePtr && nodePtr->value < object; previousNode = nodePtr, nodePtr = nodePtr->next);
if(previousNode)
previousNode->next = newNode;
else
head = newNode;
newNode->next = nodePtr;
}
}
template <class T>
int LinkedList<T>::count(){
int count = 0;
ListNode *nodePtr;
if(!head)
return count;
else{
for(nodePtr = head; nodePtr; nodePtr = nodePtr->next){
count = count + 1;
}
return count;
}
}