I keep getting the following error and I am not sure why. After some googling I still do not have a solution. I included the files below so that hopefully you all can see something that I can't.
Error:
expected class-name before '{' token class LinkedList: public ListInterface{
LinkedList.h:
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include "ListInterface.h"
#include "Node.h"
#include <stdexcept>
template <typename T>
class LinkedList: public ListInterface {
private:
Node<T>* m_front;
int m_length;
void addFront(T entry) throw(std::runtime_error);
void addBack(T entry);
void removeFront() throw(std::runtime_error);
void removeBack() throw(std::runtime_error);
public:
LinkedList();
LinkedList(const LinkedList& original);
~LinkedList();
LinkedList& operator=(const LinkedList& original);
bool isEmpty() const;
int getLength() const;
void insert(int position, T entry) throw(std::runtime_error);
void removeN(int position) throw(std::runtime_error);
void clear();
T getEntry(int position) const throw(std::runtime_error);
void replace(int position, T newEntry) throw(std::runtime_error);
};
#include "LinkedList.cpp"
#endif
LinkedList.cpp
#include "LinkedList.h"
#include "ListInterface.h"
#include "Node.h"
#include <stdexcept>
#include <iostream>
template<typename T>
LinkedList<T>::LinkedList(){
m_length = 0;
m_front = nullptr;
}
template<typename T>
LinkedList<T>::LinkedList(const LinkedList& original){
if(original.m_front == nullptr){
m_front = nullptr;
return;
}
Node<T>* tempN = new Node<T>(original.getEntry(0));
m_front = tempN;
Node<T>* tempP = m_front;
m_length = original.m_length;
for(int i = 1; i < m_length; i++){
tempN = nullptr;
tempN = new Node<T>(original.getEntry(i));
tempP->setNext(tempN);
tempP = tempP->getNext();
}
}
template<typename T>
LinkedList<T>::~LinkedList(){
clear();
if(m_front != nullptr){
delete m_front;
m_front = nullptr;
}
}
template<typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList& original){
if(original.m_front == nullptr){
m_front = nullptr;
return;
}
Node<T>* tempN = new Node<T>(original.getEntry(0));
m_front = tempN;
Node<T>* tempP = m_front;
for(int i = 1; i < original.m_length(); i++){
tempN = nullptr;
tempN = new Node<T>(original.getEntry(i));
tempP->setNext(tempN);
tempP = tempP->getNext();
}
}
template<typename T>
bool LinkedList<T>::isEmpty() const{
if(m_length == 0){
return 1;
}
return 0;
}
template<typename T>
int LinkedList<T>::getLength() const{
return m_length;
}
template<typename T>
void LinkedList<T>::insert(int position, T entry) throw(std::runtime_error){
Node<T>* temp = m_front;
Node<T>* nTemp = nullptr;
Node<T>* bTemp = m_front;
if(position > m_length){
throw(std::runtime_error("ERROR: Invalid Position\n"));
}else if(m_front == nullptr){
// throw(std::runtime_error("ERROR: Cannot insert into nonexistant list. Try addBack\n"));
addBack(entry);
}else{
if(m_length == 0){
// m_front = nTemp;
}else{
if(position == 0){
// nTemp->setNext(m_front);
// m_front = nTemp;
addFront(entry);
}else{
nTemp = new Node<T>(entry);
for(int i = 0; i < position; i++){
bTemp = bTemp->getNext();
}
for(int i = 0; i < position - 1; i++){
temp = temp->getNext();
}
temp->setNext(nTemp);
nTemp->setNext(bTemp);
m_length++;
}
}
}
temp = nullptr;
nTemp = nullptr;
bTemp = nullptr;
}
template<typename T>
void LinkedList<T>::removeN(int position) throw(std::runtime_error){
if(position == 0){
try{
removeFront();
}catch(std::runtime_error e){}
}else if(position == m_length - 1){
try{
removeBack();
}catch(std::runtime_error e){};
}else if(position < 0 || position >= m_length){
throw(std::runtime_error("ERROR: Invalid position"));
}else{
Node<T>* decon = m_front;
Node<T>* preDecon = m_front;
Node<T>* temp = m_front;
for(int i = 0; i < position - 1; i++){
preDecon = preDecon->getNext();
}
for(int i = 0; i < position; i++){
decon = decon->getNext();
}
for(int i = 0; i < position + 1; i++){
temp = temp->getNext();
}
preDecon->setNext(temp);
decon->setNext(nullptr);
delete decon;
decon = nullptr;
preDecon = nullptr;
temp = nullptr;
m_length--;
}
}
template<typename T>
void LinkedList<T>::clear(){
while(m_length > 0){
try{
removeBack();
}catch(std::runtime_error e){
break;
}
}
m_front = nullptr;
}
template<typename T>
T LinkedList<T>::getEntry(int position) const throw(std::runtime_error){
if(position >= m_length){
throw(std::runtime_error("ERROR: Invalid position\n"));
}
Node<T>* temp = m_front;
for(int i = 0; i < position; i++){
temp = temp->getNext();
}
return temp->getEntry();
}
template<typename T>
void LinkedList<T>::replace(int position, T newEntry) throw(std::runtime_error){
if(position >= m_length){
throw(std::runtime_error("ERROR: Invalid position\n"));
}
Node<T>* temp = m_front;
for(int i = 0; i < position; i++){
temp = temp->getNext();
}
temp->setEntry(newEntry);
}
template<typename T>
void LinkedList<T>::addFront(T entry) throw(std::runtime_error){
if(m_front == nullptr){
throw(std::runtime_error("ERROR: Cannot add to nonexistant list. Try addBack\n"));
}
Node<T>* temp = new Node<T>(entry);
temp->setNext(m_front);
m_front = temp;
m_length++;
temp = nullptr;
}
template<typename T>
void LinkedList<T>::addBack(T entry){
if(m_front == nullptr){
m_front = new Node<T>(entry);
m_length++;
}else{
Node<T>* temp = new Node<T>(entry);
Node<T>* mtemp = m_front;
while(mtemp->getNext() != nullptr){
mtemp = mtemp->getNext();
}
mtemp->setNext(temp);
m_length++;
temp = nullptr;
delete temp;
mtemp = nullptr;
}
}
template<typename T>
void LinkedList<T>::removeFront() throw(std::runtime_error){
if(m_length == 0){
throw(std::runtime_error("ERROR: Nothing to remove"));
}
Node<T>* temp = m_front;
m_front = temp->getNext();
delete temp;
temp = nullptr;
m_length--;
}
template<typename T>
void LinkedList<T>::removeBack() throw(std::runtime_error){
if(m_length <= 0){
throw(std::runtime_error("ERROR: Nothing to remove"));
}
Node<T>* temp = m_front;
for(int i = 0; i < m_length - 2; i++){
temp = temp->getNext();
}
if(m_length == 1){
delete temp;
m_front = nullptr;
}else{
delete temp->getNext();
temp->setNext(nullptr);
}
m_length--;
}
ListInterface:
#ifndef LIST_INTERFACE_H
#define LIST_INTERFACE_H
#include<stdexcept>
template <typename T>
class ListInterface
{
public:
/**
* @pre None
* @post Deletes a List
* @return None
*/
virtual ~ListInterface(){}
/**
* @pre None
* @post Checks to see if the list has contents
* @return None
*/
virtual bool isEmpty() const = 0;
/**
* @pre None
* @post Determines and returns list length
* @return m_length
*/
virtual int getLength() const = 0;
/**
* @pre 0<= position < m_length
* @post Adds a new item to the list at the given position
* @param position 0<= position < m_length
* @param entry Valid type T
* @throw std::runtime_error if position is invalid
* @return None
*/
virtual void insert(int position, T entry) throw (std::runtime_error) = 0;
/**
* @pre List must have items 0<= position < m_length
* @post Removes an item from the list
* @param position 0<=position< m_length
* @throw std::runtime_error if position is invalid
* @return None
*/
virtual void removeN(int position) throw (std::runtime_error) = 0;
/**
* @pre List has items in it
* @post Removes all list items
* @return None
*/
virtual void clear() = 0;
/**
* @pre Items exist in list 0<=position < m_length
* @post Returns the value of an item in the list
* @param position 0<=position < m_length
* @throw std::runtime_error if position is invalid
* @return Type T of entry
*/
virtual T getEntry(int position) const throw (std::runtime_error) = 0;
/**
* @pre The position is between 1 and the list's length
* @post The entry at the given position is replaced with the new entry
* @param position: 0<= position <= length
* @param newEntry: A new entry to put in the list
* @throw std::runtime_error if the position is invalid.
* @return None
*/
virtual void replace(int position, T newEntry) throw (std::runtime_error) = 0;
};
#endif