Here is my code:

slist.hpp

#ifndef SLIST_HPP
#define SLIST_HPP

#include"slist.h"
#include"slistiterator.h"
#include"slistiterator.hpp"

template<typename generic>
SList<generic>::SList()
{
  m_size = 0;
  m_front = NULL;
}

template<typename generic>
SList<generic>::SList(const SList<generic>& s) : m_front(NULL), m_size(0)
{
  *this = s;
}

template<typename generic>
SList<generic>::~SList()
{
  clear();
}

template<typename generic>
void SList<generic>::push_front(generic x)
{
  SNode<generic>* temp = new SNode<generic>;
  temp->b = m_front;
  m_front = temp;
  m_front->data = x;
  m_size++;
}

template<typename generic>
void SList<generic>::pop_front()
{
  if(m_front != NULL)
  {
    SNode<generic>* temp = new SNode<generic>;
    temp = m_front;    m_front = m_front->b;
    delete temp;
    m_size--;
  }
}

template<typename generic>
void SList<generic>::remove(generic x)
{
  if(m_front != NULL)
  {
    SNode<generic>* temp1 = new SNode<generic>;
    SNode<generic>* temp2 = new SNode<generic>;
    do
    {
      temp1 = m_front;
      while(temp1 != NULL && temp1->b->data != x)
      {
        temp1 = temp1->b;
      }
      if(temp1 != NULL)
      {
        temp2 = temp1->b->b;
        delete temp1->b;
        m_size--;
        temp1->b = temp2;
      }
    }while(temp1 != NULL && temp2 != NULL);
  }
}

template<typename generic>
generic& SList<generic>::front()
{
  if(m_front = NULL)
  {
    throw out_of_range
    (
      "There is no data to return"
    );
  }
  return *m_front;
}
template<typename generic>
const generic& SList<generic>::front() const
{
  if(m_front = NULL)
  {
    throw out_of_range
    (
      "There is no data to return"
    );
  }
  return *m_front;
}

template<typename generic>
void SList<generic>::clear()
{
  while(m_front != NULL)
  {
    pop_front();
  }
}

template<typename generic>
unsigned int SList<generic>::size() const
{
  return m_size;
}

template<typename generic>
bool SList<generic>::empty() const
{
  return (m_front == NULL);
}

template<typename generic>
Iterator SList<generic>::begin() const
{
  return Iterator(m_front);
}

template<typename generic>
Iterator SList<generic>::end() const
{
  return Iterator();
}

template<typename generic>
SList<generic>& SList<generic>::operator=(const SList<generic>& s)
{
  clear();
  if(!s.empty())
  {
    m_front = new SNode<generic>;
    SNode<generic>* temp = m_front;
    for(Iterator i = s.begin(); i != s.end(); m_size++, temp = temp->b)
    {
      temp->data = *i;
      i++ != s.end() ? temp->b = new SNode<generic> : temp->b = NULL;
    }
  }
  return *this;
}

#endif

slist.h

/*
  Class: SList
  A singly-linked list container.

  Constructor: SList()
  Initializes the size to 0 and the front to null.

  Constructor: SList(const SList& s)
  Clears the contents of the list and then creates a duplicate of s.

    Parameters:
    s - The list to be copied.

  Destructor: ~SList()
  Deletes the entire contents of the list.

  Function: void push_front(generic x)
  Inserts the data item x to the front of the list.

    Parameters:
    x - The data item to be inserted.

  Function: void pop_front()
  Removes a data item from the front of the list. If there are no items to
  remove, then the function does nothing.

  Function: void remove(generic x)
  Removes all instances of x from the list. If there are no data items
  containing x to remove, then the function does nothing.

    Parameters:
    x - The data item to be removed.

  Function: generic& front()
  Retreives a writeable reference to the data item at the front of the list. If
  the list is empty then a standard out_of_range exception is thrown.

    Returns:
    A writeable reference to the list's front data.

  Function: const generic& front() const
  Retreives a read-only reference to the data item at the front of the list. If
  the list is empty then a standard out_of_range exception is thrown.
 Returns:
    A read-only reference to the list's front data.

  Function: void clear()
  Deletes the entire contents of the list and sets size back to 0.

  Function: unsigned int size() const
  Reports the number of data items in the list.

    Returns:
    The number of items stored in the list.

  Function: bool empty() const
  Reports if the list is holding data or is not holding any data.

    Returns:
    True if the list has no data and false if it has data.

  Function: Iterator begin() const
  Places an iterator at the front of the list.

    Returns:
    An iterator positioned at the front of the list.

  Function: Iterator end() const
  Places an iterator at the back of the list.

    Returns:
    An iterator positioned at the back of the list.

  Operator: SList& operator=(const SList& s)
  Clears the contents of the list and then creates a duplicate of s.

    Parameters:
    s - The list to be replicated.

    Returns:
    A reference to the newly cloned list.
*/
#ifndef SLIST_H
#define SLIST_H

#include"snode.h"
#include"slistiterator.h"
#include<stdexcept>
using std::out_of_range;

template<typename generic>
class SList
{
 public:
  SList();
  SList(const SList& s);
  ~SList();
  void push_front(generic x);
  void pop_front();
  void remove(generic x);
  generic& front();
  const generic& front() const;
  void clear();
  unsigned int size() const;
  bool empty() const;
  typedef SListIterator<generic> Iterator;
  Iterator begin() const;
  Iterator end() const;
  SList& operator=(const SList& s);

 private:
  unsigned int m_size;
  SNode<generic>* m_front;
};

#include"slist.hpp"
#endif

This should be all of the relevant code, but If you need more just say so. Anyway the error is in slist.hpp. It occurs at the two functions that return Iterators, lines 121 and 127.

Recommended Answers

All 4 Replies

I would try defining the Iterator type with a typename:

//in SList class declaration:
  typedef typename SListIterator<generic> Iterator;

Please post the entire error message (it is cut-off in the thread's title).

Here is the full error:

slist.hpp:121: error: expected constructor, destructor, or type conversion before âSListâ
slist.hpp:127: error: expected constructor, destructor, or type conversion before âSListâ

I tried adding typename, but it didn't seem to work

Where's the definition for SListIterator?

here is slistiterator.hpp:

#ifndef SLISTITERATOR_HPP
#define SLISTITERATOR_HPP

template<typename generic>
SListIterator<generic>::SListIterator() : m_current(NULL){}

template<typename generic>
SListIterator<generic>::SListIterator(SNode<generic>* s) : m_current(s){}

template<typename generic>
generic& SListIterator<generic>::operator*()
{
  if(m_current == NULL)
  {
    throw out_of_range("the iterator is not currently on a node");
  }
  return m_current->data;
}

template<typename generic>
const generic& SListIterator<generic>::operator*() const
{
  if(m_current == NULL)
  {
    throw out_of_range("the iterator is not currently on a node");
  }
  return m_current->data;
}

template<typename generic>
SListIterator<generic> SListIterator<generic>::operator++()
{
  if(m_current != NULL)
  {
    m_current = m_current->b;
  }
  return *this;
}

template<typename generic>
SListIterator<generic> SListIterator<generic>::operator++(int)
{
  return ++(*this);
}

template<typename generic>
bool SListIterator<generic>::operator==(const SListIterator& rhs) const
{
  return m_current == rhs.m_current;
}

template<typename generic>
bool SListIterator<generic>::operator!=(const SListIterator& rhs) const
{
  return m_current != rhs.m_current;
}

#endif

And here is the header file:

/*
  Class: SListIterator
  A singly-linked list iterator.

  Constructor: SListIterator()
  Initializes m_current to null.

  Constructor: SListIterator(SNode<generic>* s)
  Points m_current to s.

    Parameters:
    s - The data item which m_current should point to.

  Operator: generic& operator*()
  Retreives a writable reference to the data item at the iterator's position.
  If the iterator is not located at a data item, the function throws a standard
  out_of_range error.

    Returns:
    A writable reference to the iterator's data.

  Operator: const generic& operator*() const
  Retreives a read-only reference to the data item at the iterator's position.
  If the iterator is not located at a data item, the function throws a standard
  out_of_range error.

    Returns:
    A read-only reference to the iterator's data.

  Operator: SListIterator operator++()
  A pre-increment operator that moves the iterator to the next data item. If
  the iterator has already surpassed the last data item, then the function
  does nothing.

    Returns:
    A copy of an iterator located at the next data item.

  Operator: SListIterator operator++(int x)
  A post-increment operator that moves the iterator to the next data item. If
  the iterator has already surpassed the last data item, then the function
  does nothing.

    Parameters:
 x - A dummy variable used to differentiate between the pre and post ops.

    Returns:
    A copy of an iterator located at the next data item.

  Operator: bool operator==(const SListIterator& s) const
  A comparison operator used to determine if two iterators are the same.

    Parameters:
    s - The iterator to be compared against.

    Returns:
    True if the two iterators are positioned over the same data item and false
    if they are positioned over different data items.

  Operator: bool operator!=(const SListIterator& s) const
  A comparison operator used to determine if two iterators are different.

    Parameters:
    x - The iterator to be compared against.

    Returns:
    True if the two iterators are positioned over different data items and
    false if they are positioned over the same data item.
*/
#ifndef SLISTITERATOR_H
#define SLISTITERATOR_H

#include<stdexcept>
using std::out_of_range;
#include"snode.h"

template<typename generic>
class SListIterator
{
 public:
  SListIterator();
  SListIterator(SNode<generic>* s);
  generic& operator*();
  const generic& operator*() const;
  SListIterator operator++();
  SListIterator operator++(int x);
  bool operator==(const SListIterator& s) const;
  bool operator!=(const SListIterator& s) const;

 private:
  SNode<generic>* m_current;
};

#include"slistiterator.hpp"
#endif
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.