Reputation: 1883
I am trying to create a LinkedList class with polymorphism and inheritance from Stack and Queue header files. This is the first time I've tried to use templates but keep getting errors like "class template has already been defined" or "abstract class type is not allowed". How do I implement a template since I'm obviously doing it incorrectly?
// LinkedList.h File
#include "Stack.h"
#include "Queue.h"
#include "Node.h"
using namespace std;
template <typename T>
class LinkedList : public Queue<T>, public Stack<T>
{
public:
LinkedList();
~LinkedList(void);
protected:
Node<T> *first;
Node<T> *last;
int numItems;
};
// LinkedList.cpp File
#include "LinkedList.h"
using namespace std;
template <typename T>
class LinkedList
{
LinkedList()
{
first = NULL;
last = NULL;
numItems = 0;
}
LinkedList::~LinkedList(void)
{
while (first != NULL)
{
Node* cur = first;
first = first->next;
delete cur;
}
}
LinkedList::clear() {}
LinkedList::size() {}
// Stack Functions
LinkedList::push(T item) {}
LinkedList::pop() {}
LinkedList::top() {}
// Queue Functions
LinkedList::enqueue(T item) {}
LinkedList::dequeue() {}
LinkedList::peek() {}
}
// Stack.h File (Queue is the same except push/pop/top = enqueue/dequeue/peek)
#pragma once
template <typename T> class Stack
{
public:
virtual ~Stack() {}
virtual int size() = 0;
virtual void clear() = 0;
virtual void push(T item) = 0;
virtual T pop() = 0;
virtual T top() = 0;
};
Updated Code
// LinkedList.h File
#pragma once
#include "Stack.h"
#include "Queue.h"
#include "Node.h"
template <typename T>
class LinkedList : public Queue<T>, public Stack<T>
{
public:
LinkedList();
~LinkedList(void);
void clear();
int size();
void push(T item);
T pop();
T top();
void enqueue(T item);
T dequeue();
T peek();
protected:
Node<T> *first;
Node<T> *last;
int numItems;
};
// LinkedList.cpp File
#include "LinkedList.h"
class LinkedList
{
LinkedList::LinkedList()
{
first = NULL;
last = NULL;
numItems = 0;
}
LinkedList::~LinkedList(void)
{
while (first != NULL)
{
Node* cur = first;
first = first->next;
delete cur;
}
}
LinkedList::clear(){}
LinkedList::size(){}
// Stack Functions
void LinkedList::push(T item){}
T LinkedList::pop(){}
T LinkedList::top(){}
// Queue Functions
void LinkedList::enqueue(T item){}
T LinkedList::dequeue(){}
T LinkedList::peek(){}
}
// Stack.h File (Queue is the same except push/pop/top = enqueue/dequeue/peek)
#pragma once
template <typename T> class Stack
{
public:
virtual ~Stack() {}
virtual int size() = 0;
virtual void clear() = 0;
virtual void push(T item) = 0;
virtual T pop() = 0;
virtual T top() = 0;
};
Upvotes: 0
Views: 1517
Reputation: 81349
I see
// LinkedList.cpp File
Template definitions have to be visible on every translation unit, you cannot place their definitions in a cpp file and expect it to work (like it does for regular classes). Definitions for all template functions should be present at a header file.
Also, the compiler error is correct. You are writing definitions for two LinkedList
classes. I assume you were looking for:
LinkedList::LinkedList(){ ... }
Upvotes: 1