So I have two classes, one is abstract, and one is not.
The abstract class is Iterator, and the concrete one is LinkedListIterator. The code for both is at the bottom of the post.
The issue I'm having is with the code as it is below, I get 1 error in my LinkedListIterator at the last line of the destructor saying
undefined reference to `Iterator<int>::~Iterator()'
Now I tried commenting out the virtual ~Iterator() destructor, and no errors but I get a warning saying:
Class '[C@800c1' has virtual method 'remove' but non-virtual destructor
So my question is: Do I need a virtual destructor in the abstract Iterator base class? I've read that you should always have one, but in this case the destructor in LinkedListIterator merely sets values, it doesn't free anything...
Thanks!
Iterator Code:
template<class T>
class Iterator {
public:
//~Constructors/Destructors------------//
/*
* Destroys necessary resources.
*/
virtual ~Iterator() = 0;
//~Methods-------------------//
/*
* Informs the user whether there are more elements to be iterated
* over in a List.
*
* @return true if there are more elements to iterate over, false otherwise.
*/
virtual bool hasNext() = 0;
/*
* Gets the next element to iterate over.
*
* @return the next element in the iteration.
*/
virtual T next() = 0;
/*
* Adds an element to the List being iterated over.
*
* @param element the element to add.
* @return true if successful, false otherwise.
*/
virtual bool add(T element) = 0;
/*
* Removes the element last returned by next from
* the List being iterated over.
*
* @return true if successful, false otherwise.
*/
virtual bool remove() = 0;
};
Relevant LinkedListIterator Code (it's a longish class):
template<class T>
class LinkedListIterator : public Iterator<T> {
private:
//~Data Fields---------------------------------//
/*
* Pointer to the node that the iterator is currently at.
*/
Node<T>* current;
/*
* Pointer to the LinkedList being iterated through.
*/
LinkedList<T>* list;
/*
* Boolean value indicating whether next has been called since
* the last remove operation.
*/
bool nextCalled;
public:
//~Constructors/Destructors------------------//
/*
* Constructor for LinkedListIterator, takes in a pointer to a Node
* to initialize current to point to (intended to be the head of the
* the LinkedList).
*
* @param theList pointer to the LinkedList being iterated through.
*/
LinkedListIterator(LinkedList<T>* theList) {
current = theList->head;
list = theList;
nextCalled = false;
}
/*
* Destructor, resets pointer values to 0.
*/
~LinkedListIterator() {
current = 0;
list = 0;
}