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;
		}
}

next time why don't you tell us why you think it's wrong first

how is "position" used in your method? you seem to just iterate to the end

Sorry, thought posistion would be obvious. It is the same as myList[45] the 45 would be passed to the overloaded operator. It reads it correctly if the array index is out of bounds for the linkedlist, but I do not seem to be getting the correct value passed back, because when ever I do something like myList[1]=5, the value does not change.

I have tried this, but it still does not work. Basically i want it to act just like the normal array operator, so someone could do myList[3]=4 and then it replaces array index 3 with 4. But what I have, the value of the given posistion does not change. Any advice?

template <class T>
T &LinkedList<T>::operator[](const 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;

		while(count != posistion){

			ptrNode = ptrNode->next;
			count++;
		}
	}

	//T returnObject = ptrNode->value;  used to see if it would work
	return ptrNode->value;  //returnObject;  used to see if it would work
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.