I’m trying to create my own templated List class as a practice excercise. I should have probably started smaller but there you go.. i’m getting the error “expected unqualified-id before ‘<‘ token ” at each line that reads:
typedef <typename ListType>
I’m a student and I’ve been reading up on templates for a couple of hours but still cant figure this one out, any help would be much appreciated!
My hpp file for List:
#if !defined _LIST_HPP_
#define _LIST_HPP_
#include "Node.hpp"
///since we're creating a template everything must be defined in the hpp
typedef <typename ListType>
class List
{
public:
List();
bool Empty();
void PushFront();
void PushBack();
void PopBack();
Node<ListType>& GetHead();
private:
int _size;
Node<ListType>* _head;
Node<ListType>* _tail;
};
///implement List class here
typedef <typename ListType>
List<ListType>::List() : _head(0), _tail(0), _size(0)
{
}
typedef <typename ListType>
bool <ListType>Empty()
{
return !_size; //returns true if size = 0
}
typedef <typename ListType>
void List<ListType>::PushFront()
{
_head = new Node<ListType>( _head , 0 );
if (!Empty())
_head->_prev->_next = _head; //set previous nodes _next to new _head
++_size;
}
typedef <typename ListType>
void List<ListType>::PushBack()
{
_tail = new Node<ListType>( 0 , _tail);
if (!Empty())
_tail->_next->_prev = _tail; // set old tails _prev to new tail
++_size;
}
typedef <typename ListType>
void List<ListType>::PopBack()
{
}
typedef <typename ListType>
Node<ListType>& List<ListType>::GetHead()
{
return _head;
}
#endif //define
And i’ll also include the templated node class hpp incase it could be something there that’s throwing things off?
#if !defined _NODE_HPP_
#define _NODE_HPP_
//#include "Sprite.hpp"
template<typename NodeType>
class Node{
public:
Node( Node* prev = 0, Node* next = 0);
void SetData(NodeType newData);
void GetData();
private:
friend class List;
NodeType _data;
Node* _next;
Node* _prev;
};
///implement Node
template <typename NodeType>
Node<NodeType>::Node(Node* prev, Node* next) : _prev(prev), _next(next)
{}
template <typename NodeType>
void Node<NodeType>::SetData(NodeType newData)
{
_data = newData;
}
template <typename NodeType>
void Node<NodeType>::GetData()
{
return _data;
}
#endif //define
It’s
template, nottypedef.And while we’re at it, your preprocessor tokens are invalid identifiers since they start with an underscore, which isn’t allowed here. Use something like
LIST_HPPinstead.Finally, let’s take a look at your
Emptydefinition:First of all, this is missing the class name in the method name. Secondly, I dislike the conversion from a number to
bool. Why not use an explicit comparison? That also obviates the comment, which is useless in itself since it merely paraphrases the code.This leaves us with: