I was carefully going through the examples in a book of mine. Everything was going fine until I created the SListIterator class. It keeps pointing at its constructor. I tried everything but no luck. Here are the errors:
error C2143: syntax error : missing ';' before '<'
error C2501: 'X3D::SListIterator<Datatype>::SLinkedList' : missing storage-class or type specifiers
error C2238: unexpected token(s) preceding ';'
error C2143: syntax error : missing ')' before '<'
error C2143: syntax error : missing ';' before '<'
error C2059: syntax error : ')'
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
Any help would be appreciated. Thanks.
SListNode.h:
Code:
#ifndef SLISTNODE_H
#define SLISTNODE_H
namespace X3D
{
template <typename Datatype> class SListNode
{
public:
Datatype m_data;
SListNode<Datatype> *m_next;
// Inserts a new data value in the linked list. This is
// not sorted nor put at the very end of the list.
void InsertAfter(Datatype data)
{
// create the new node and assign its given value.
SListNode<Datatype> *newNode = new SListNode<Datatype>;
newNode->m_data = data;
// have the new node point to the next node.
newNode->m_next = m_next;
// make the previous node point to the new node.
m_next = newNode;
}
};
}
#endif
SLinkedList.h:
Code:
#ifndef SLINKEDLIST_H
#define SLINKEDLIST_H
#include "SListNode.h"
#include "SListIterator.h"
namespace X3D
{
template <typename Datatype> class SLinkedList
{
public:
SListNode<Datatype> *m_head;
SListNode<Datatype> *m_tail;
int m_count;
SLinkedList()
{
m_head = 0;
m_tail = 0;
m_count = 0;
}
~SLinkedList()
{
SListNode<Datatype> *itr = m_head;
SListNode<Datatype> *next;
while (itr != 0)
{
// save the pointer to the next node
next = itr->m_next;
// delete the current node
delete itr;
// make the next node the current node
itr = next;
}
}
void Append(Datatype data)
{
if (m_head == 0)
{
m_head = m_tail = new SListNode<Datatype>;
m_head->m_data = data;
}
else
{
// insert a new node after the tail and reset the tail.
m_tail->InsertAfter(data);
m_tail = m_tail->m_next;
}
m_count++;
}
// adds a new node at the beginning of the list.
void Prepend(Datatype data)
{
// create a new node
SListNode<Datatype> *newNode = new SListNode<Datatype>;
newNode->m_data = data;
newNode->m_next = m_head;
// set the head node and the tail node if needed.
m_head = newNode;
if (m_tail == 0)
m_tail = m_head;
m_count++;
}
void RemoveHead()
{
SListNode<Datatype> *node = 0;
if (m_head != 0)
{
node = m_head->m_next;
delete m_head;
m_head = node;
if (m_head == 0)
m_tail = 0;
m_count--;
}
}
void RemoveTail()
{
SListNode<Datatype> *node = m_head;
while (m_head != 0)
{
if (m_head == m_tail)
{
delete m_head;
m_head = m_tail = 0;
}
else
{
while (node->m_next != m_tail)
node = node->next;
m_tail = node;
delete node->m_next;
node->m_next = 0;
}
m_count--;
}
}
// Creates a new iterator pointing to the head in the
// current list.
SListIterator<Datatype> GetIterator()
{
return (SListIterator<Datatype>(this, m_head));
}
};
}
#endif
SListIterator.h:
Code:
#ifndef SLISTITERATOR_H
#define SLISTITERATOR_H
#include "SLinkedList.h"
namespace X3D
{
template <typename Datatype> class SListIterator
{
public:
SListNode<Datatype> *m_node;
SLinkedList<Datatype> *m_list;
// Initializes the iterator.
SListIterator(SLinkedList<Datatype> *list = 0,
SListNode<Datatype> *node = 0)
{
m_list = list;
m_node = node;
}
// Resets the iterator to the head node of the list.
void Start()
{
if (m_list != 0)
m_node = m_list->m_head;
}
// Moves the iterator towards the next node in the list.
void Forth()
{
if (m_list != 0)
m_node = m_node->m_next;
}
// Returns a reference of the item stored in the node
// that the iterator is pointing to.
Datatype& Item()
{
return (m_node->m_data);
}
// Returns true if the current node being pointed to is
// valid. Returns false otherwise.
bool Valid()
{
return (m_node != 0);
}
};
}
#endif