I am having an issue with my nested class. The file uses template classes to implement a queue but has a compile error that I cannot understand. My personal research into the error shows that it has something to do with the declaration of the template valuename but I am not sure what I am doing wrong.
Here is the source code for the class: queue.h
#ifndef QUEUE_H
#define QUEUE_H
template<class T>
class Queue
{
private:
template<class U>
class Node
{
private:
U _item;
Node* _next;
public:
Node(U item);
~Node();
U getItem();
Node<U>* getNext();
void setNext(Node<U>* next);
};
unsigned _size;
Node<T>* _head;
Node<T>* _tail;
public:
Queue();
~Queue();
Node<T>* getHead();
Node<T>* getTail();
unsigned size();
bool isEmpty();
void push_back(T item);
T pop();
void combine(Queue<T> qu);
};
//////////////////////////////////
//NODE
//////////////////////////////////
template<class T>
template<class U>
Queue<T>::Node<U>::Node(U item)
//constructor
{
_item = item;
_next = NULL;
}
template<class T>
template<class U>
Queue<T>::Node<U>::~Node()
//destructor
{
}
template<class T>
template<class U>
U Queue<T>::Node<U>::getItem()
//returns the provided item.
{
return _item;
}
template<class T>
template<class U>
Node<U>* Queue<T>::Node<U>::getNext()
//returns a pointer to the next node in the queue.
{
return _next;
}
template<class T>
template<class U>
void Queue<T>::Node<U>::setNext(Node<U>* next)
//sets the next node in the queue to the provided Node
{
_next = next;
}
//////////////////////////////////
//QUEUE
//////////////////////////////////
template<class T>
Queue<T>::Queue()
//constructor
{
_size = 0;
_head = NULL;
_tail = NULL;
}
template<class T>
Queue<T>::~Queue()
//destructor
{
while(!isEmpty()){
pop();
}
}
template<class T>
Node<T>* Queue<T>::getHead()
//returns the node at the front of the queue
{
return _head;
}
template<class T>
Node<T>* Queue<T>::getTail()
//returns the node at the end of the queue
{
return _tail;
}
template<class T>
unsigned Queue<T>::size()
//returns the size of the queue
{
return _size;
}
template<class T>
bool Queue<T>::isEmpty()
//if the queue is empty return true, otherwise return false
{
if(_size == 0){
return true;
}
return false;
}
template<class T>
void Queue<T>::push_back(<T> item)
//add a node to the back of the queue with the provided data item
{
Node<T>* nnode = new Node<T>(item);
if(_head == NULL){
_head = nnode;
_tail = nnode;
}
_tail.setNext(nnode);
_tail = nnode;
_size++;
}
template<class T>
T Queue<T>::pop()
//removes the value at the front of the queue and returns it
{
Node<T>* cur = _head;
_head = cur->getNext();
_size--;
<T> item = cur.getItem();
delete cur;
return item;
}
template<class T>
void Queue<T>::combine(Queue<T> qu)
//adds the provided queue to the end of the current queue
{
_tail.setNext(qu.getHead());
_tail = qu.getTail();
_size+= qu.size();
}
#endif
The error I receive is:
./queue.h:63:1: error: unknown type name 'Node'
Node<U>* Queue<T>::Node<U>::getNext()
^
./queue.h:63:5: error: expected unqualified-id
Node<U>* Queue<T>::Node<U>::getNext()
^
If you have any suggestions I would be glad to hear them.
Change:
Node<U>* Queue<T>::Node<U>::getNext()
into:
typename Queue<T>::template Node<U>* Queue<T>::Node<U>::getNext()
and others as well:
Node<T>* Queue<T>::getHead()
into:
typename Queue<T>::template Node<T>* Queue<T>::getHead()
as well as this strange:
void Queue<T>::push_back(<T> item)
into:
void Queue<T>::push_back(T item)
and last but not least:
T item = cur.getItem();
into:
T item = cur->getItem();
In c++11 nested types do not need a scope if they are specified in a trailing return type, so instead:
typename Queue<T>::template Node<U>* Queue<T>::Node<U>::getNext()
you can just say:
auto Queue<T>::Node<U>::getNext() -> Node<U>*
In c++14 just say auto
and shorten your original declaration by half:
auto Queue<T>::Node<U>::getNext()