Sign in to follow this  
Prog101

Linked list error

Recommended Posts

I am doing a linked list but i keep getting the following error:
error C2039: 'GetIterator' : is not a member of 'SLinkedList<DataType>'
        with
        [
            DataType=int
        ]


it points to this bit of the coode SListIterator<int> itr = list.GetIterator(); here is the code
/////////////////////////////////////////////////////////////////////////////////////
///// SListNode class
/////////////////////////////////////////////////////////////////////////////////////
template <class DataType>
class SListNode
{
public:
	DataType m_data;
	SListNode<DataType>* m_next;
 
	//------------------------------------------------------------
	void InsertAfterNode(DataType p_data)
	{
		//create a new node
		SListNode<DataType>* newnode = new SListNode<DataType>;
		newnode->m_data = p_data;
		//make the node point the the next node
		newnode->m_next = m_next;
		m_next = newnode;
	}
};
/////////////////////////////////////////////////////////////////////////////////////
///// SLinkedList class
/////////////////////////////////////////////////////////////////////////////////////
template <class DataType>
class SLinkedList
{
public:
	SListNode<DataType>* m_head;
	SListNode<DataType>* m_tail;
	int m_count;
	//-------------------
	// CONSTRUCTOR
	//-------------------
	SLinkedList()
	{
		m_head = 0;
		m_tail = 0;
		m_count = 0;
	}
	//-------------------
	// DESTRUCTOR
	//-------------------
	~SLinkedList()
	{
		// temp node pointers
		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;
		}
	}
 
	//---------------------------------------------
	// APPEND (ADD MORE NODES TO THE END OF LIST)
	//---------------------------------------------
	void Append(DataType p_data)
	{
		if (m_head == 0)
		{
			//create a new head node
			m_head = m_tail = new SListNode<DataType>;
			m_head->m_data = p_data;
		}
		else
		{
			//insert a new node after the tail
			m_tail->InsertAfterNode(p_data);
			m_tail = m_tail->m_next;
		}
		m_count++;
	}
	//------------------------------------------------
	// PREPEND (ADD MORE NODES TO BEGINNING OF LIST)
	//------------------------------------------------
	void Prepend(DataType p_data)
	{
		//create a new node
		SListNode<DataType>* newnode = new SListNode<DataType>;
		newnode->m_data = p_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++;
	}
	//------------------------------------------------
	// REMOVEHEAD (REMOVE NODES FROM THE BEGIINNING OF LIST)
	//------------------------------------------------
	void RemoveHead(DataType p_data)
	{
		//create a new node
		SListNode<DataType>* node = 0;
		if (m_head ! = 0)
		{
			// make node point to the next node
			node = m_head->m_next;
			// then delete the head and make the pointer point to node
			delete m_head;
			m_head = node;
			// if the head is null, the you have just deleted the only node in the list. set tail to 0
			if (m_head == 0)
				m_tail = 0;
			m_count--;
		}
	}
	//------------------------------------------------
	// REMOVETAIL (REMOVE NODES FROM THE END OF LIST)
	//------------------------------------------------
	void RemoveTail()
	{
		//create a new node
		SListNode<DataType>* node = m_head;
		// if the list isn't empty, then remove a node
		if (m_head ! = 0)
		{
			//if the head is equal to the tail, then the list has 1 node.
			// and your removing it.
			if (m_head == m_tail)
			{
				//delete the node and set both pointers to 0
				delete m_head;
				m_head = m_tail = 0;
			}
			else
			{
				// skip ahead until you find the node, right before the tail node
				while (node->m_next != m_tail)
				{
					node = node->m_next;
					//make the tail point to the node before the current tail and delete the old tail.
					m_tail = node;
					delete node->m_next;
					node->m_next = 0;
				}
				m_count--;
			}
		}
	}
};
/////////////////////////////////////////////////////////////////////////////////////
///// Iterator  class
/////////////////////////////////////////////////////////////////////////////////////
template <class DataType>
class SListIterator
{
public:
	SListNode<DataType>* m_node;
	SLinkedList<DataType>* m_list;
	//-------------------
	// CONSTRUCTOR
	//-------------------
	SListIterator(SLinkedList<DataType>* p_list = 0, SListNode<DataType>* p_node = 0)
	{
		m_list = p_list;
		m_node = p_node;
	}
	//------------------------------------------------
	// START (RESETS THE ITERATOR TO POINT TO THE 1ST NODE IN THE LIST)
	//------------------------------------------------
	void Start()
	{
		if(m_list!= 0)
			m_node = m_list->m_head;
	}
	//------------------------------------------------
	// FORTH (MOVE THE ITERATOR TO THE NEXT NODE IN THE LIST)
	//------------------------------------------------
	void Forth()
	{
		if(m_node!= 0)
			m_node = m_node->m_next;
	}
	//------------------------------------------------
	// ITEM (RETURN REFERENCE TO THE ITEM STORED)
	//------------------------------------------------
	DataType& Item()
	{
		return m_node->m_data;
	}
	//------------------------------------------------
	// VALID (CHECKS TO SEE IF ITERATOR IS POINTING TO A NON 0 NODE)
	// IF SO THEN IT RETURNS TRUE, OTHERWISE FALSE.
	//------------------------------------------------
	bool Valid()
	{
		return (m_node !=0);
	}
	//------------------------------------------------
	// GETITERATOR (GENERATES ITERTOR POINTING TO HEAD OF CURRENT LIST)
	//------------------------------------------------
	SListIterator<DataType> GetIterator()
	{
		return SListIterator<DataType> (this, m_head);
	}
};
 
 
 
 
//////////////////////////////////////////////
// Main function (where the action is at)
/////////////////////////////////////////////
int main()
{	
 
	//Create a new linked list
	SLinkedList<int> list;
	//insert 20, 20 and 30
	list.Append(10);
	list.Append(20);
	list.Append(30);
	cout << "the list contains: ";
	//create a new iterator and make it point to the beginning of the list
	SListIterator<int> itr = list.GetIterator();
	//loop through the list while the iterator is valid
	for (itr.Start();itr.Valid();itr.Forth())
	{
		cout << itr.Item() << ", ";
	}
	//reset the iterator to the beginning again
	itr.Start();
 
 
 
	cout << "Press enter to Exit" <<endl;
	cin.ignore(std::cin.rdbuf()->in_avail() + 1);
 
	return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Prog101
I am doing a linked list but i keep getting the following error:
*** Source Snippet Removed ***
it points to this bit of the coode SListIterator<int> itr = list.GetIterator();
here is the code
*** Source Snippet Removed ***
Did you even read the error message? GetIterator() isn't a member of the SLinkedList class (It's a member of the SListIterator class)

Share this post


Link to post
Share on other sites
Quote:
Original post by Prog101
i know that i followed this out of a book and they must have got it wrong hoow can i fix this


Move the GetIterator() function into the SLinkedList class?

Share this post


Link to post
Share on other sites
You want to do as Evil Steve told you, but you also need to declare the SListIterator template class before the SLinkedList class:

template< class > class SListIterator;

template <class DataType>
class SLinkedList {
...
};



I hope you are doing this as a learning exercise, though. Beyond a fair guarantee of correctness, std::list actually follows the standard interface for C++ containers and iterators, allowing you to use all applicable algorithms.


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by jflanglois
You want to do as Evil Steve told you, but you also need to declare the SListIterator template class before the SLinkedList class:
Whoops, forgot about that.

Quote:
Original post by jflanglois
I hope you are doing this as a learning exercise, though. Beyond a fair guarantee of correctness, std::list actually follows the standard interface for C++ containers and iterators, allowing you to use all applicable algorithms.
Also, is this code from the book's CD, or from the book itself? A lot of the time, code in the book is full of typos, but it's generally correct on the CD (One would hope so).
I agree with jflanglois to a certain extent. It's good to learn how to implement a linked list yourself, as it's a fudamental data structure, however for pretty much all normal use (General programming, game development, and even game development on consoles) you'll be using std::list because it's well optimised, generic, and most importantly, standard.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this