Thread: Linked List!!!Error in Clear() function!!please help me..

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    52

    Post Linked List!!!Error in Clear() function!!please help me..

    My Other functions are ok..But Clear() function has a error...When I compile,It informs that
    "error C2662: 'List<Entry>::Remove' : cannot convert 'this' pointer from 'const List<Entry>' to 'List<Entry> &"
    I don't understand why compiler informs this error....
    With applications "Stack" or "Queue",I use "Remove" function in "Clear()" function,it is Ok...
    But with "List", compiler informs this error...
    "Remove()" function is Ok,it doesn't have error....


    This is my code..please help me!!thanks very much...

    "List.h"

    Code:
    #ifndef LIST_H
    #define LIST_H
    
    #include "NodeList.h"
    enum ErrorCode{sucess, underflow, overflow, errorrange, errorcode};
    template <class Entry>
    class List{
    	public:
    	List();
    	~List();
    	List(const List<Entry> &);
    	List<Entry> &operator=(const List<Entry> &);
    	int Size() const;
    	void Clear() const;
    	bool Empty() const ;
    	bool Full() const;
    	ErrorCode Insert(const Entry &item = 0, int position = 0);
    	ErrorCode Remove(Entry &item, int position = 0);
    	ErrorCode Replace(const Entry &item = 0 ,  int position = 0 );
    	ErrorCode Retrieve(Entry &item , int position = 0);
    	void Traverse(void (*duyet)(Entry&x));
    	private:
    		int count;
    		Node<Entry> * pchinh;
    		mutable int CurrentPosition;
    		mutable Node<Entry> * pcurrent;
    	protected:
    		void SetPosition(int position) const;
    };
    
    #endif
    "NodeList.h"

    Code:
    #ifndef NODELIST_H
    #define NODELIST_H
    template <class Entry>
    struct Node{
    	Node();
    	Entry entry;
    	Node<Entry> * next;
    	Node(const Entry &item, Node<Entry> * p =NULL);
    };
    #endif
    "NodeList.cpp"

    Code:
    #ifndef NODELIST_CPP
    #define NODELIST_CPP
    #include "NodeList.h"
    template <class Entry>
    Node<Entry>::Node(){
    	next = NULL;
    }
    
    template <class Entry>
    Node<Entry>::Node(const Entry & item, Node<Entry> * p){
    	entry = item;
    	next = p;
    }
    
    #endif
    "List.cpp"
    Code:
    #ifndef LIST_CPP
    #define LIST_CPP
    #include "List.h"
    
    template <class Entry>
    List<Entry>::List(){
    	count = 0;
    	pchinh = NULL;
    	pcurrent = NULL;
    	CurrentPosition = 0;
    }
    
    template <class Entry>
    List<Entry>::~List(){
    	int i;
    	while(!Empty())
    		Remove(i);
    }
    	
    
    template <class Entry>
    List<Entry>::List(const List<Entry> & list0){
    	Node<Entry> *psao, * pduyet = list0.pchinh;
    	if (pduyet==NULL) pchinh = NULL;
    	else
    	{
    		pchinh = psao = new Node<Entry> (pduyet->entry);
    		while (pduyet->next != NULL)
    		{
    			pduyet = pduyet->next;
    			psao->next = new Node<Entry>(pduyet->entry);
    			psao = psao-> next;
    		}
    	}
    	return sucess;
    }
    
    template <class Entry>
    List<Entry>& List<Entry>::operator=(const List<Entry> & list0){
    	Node <Entry> *ptam, *psao, *pduyet = list0.pchinh;
    	int i;
    	count = 1;
    	if (pduyet==NULL) ptam = NULL;
    	else
    	{
    		ptam = psao = new Node<Entry> (pduyet->entry);
    		while(pduyet->next!= NULL)
    		{
    			pduyet = pduyet->next;
    			psao->next = new Node<Entry>(pduyet->entry);
    			psao = psao -> next;
    			count++;
    		}
    	}
    	while(!Empty())
    		Remove(i);
    		pchinh = ptam;
    		return (*this);
    }
    
    template <class Entry>
    ErrorCode List<Entry>::Insert(const Entry&item, int position){
    	Node <Entry> *ptruoc, *psau, *newnode = new Node<Entry>(item);
        if (newnode == NULL)
    	{
    		 cout<<"Da het bo nho!Khong cap phat duoc nua.";
    		 return overflow;
    	}
    	if (position < 0 || position > count)
    	{
    		cout<<"Vi tri chen khong hop le!";
    		return errorrange;
    	}
    
    	if (position == 0)
    	{
    		if (pchinh ==NULL)
    		{
    		pchinh = newnode;
    		pcurrent = pchinh;
    		count++;
    		return sucess;
    		}
    		else
    		{
    		newnode -> next = pchinh;
    		pchinh = newnode;
    		pcurrent = pchinh;
    		count++;
    		return sucess;
    		}
    	}
    	else
    	SetPosition(position-1);
    	ptruoc = pcurrent;
    	psau = ptruoc->next;
    	newnode->next = psau;
    	ptruoc -> next = newnode;
    	count++;
    	return sucess;
    }
    
    template <class Entry>
    ErrorCode List<Entry>::Remove(Entry &item, int position){
    	Node<Entry> * psau, *ptruoc, *ptam = pchinh;
    	if (position < 0 || position >= count)
    	{
    		cout<<"Vi tri xoa khong hop le!";
    		return errorrange;
    	}
    	if (position == 0)
    		if (pchinh == NULL)
    		{
    			cout<<"Chua co phan tu nao de xoa!";
    			return errorcode;
    		}
    		else
    		{
    		pchinh = pchinh->next;
    		pcurrent = pchinh;
    		count--;
    		item = ptam->entry;
    		delete ptam;
    		return sucess;
    		}
    	else
    		SetPosition(position-1);
    		ptruoc = pcurrent;
    		psau = ptruoc->next;
    		ptam = psau;
    		ptruoc->next = psau -> next;
    		item = ptam->entry;
    		delete ptam;
    		count--;
    		return sucess;
    }
    
    template <class Entry>
    ErrorCode List<Entry>::Replace(const Entry &item, int position){
    	if (position < 0 || position > count)
    	{
    		cout<<"Vi tri thay the khong hop le!";
    		return errorrange;
    	}
    
    	if (position == 0)
    		if (pchinh==NULL)
    		{
    			cout<<"Chua co phan tu nao de thay the!";
    			return errorcode;
    		}
    		else
    		{
    		pchinh->entry = item;
    		return sucess;
    		}
    	else
    	SetPosition(position);
    	pcurrent->entry = item;
    	return sucess;
    }
    
    template <class Entry>
    ErrorCode List<Entry>::Retrieve(Entry &item,int position){
    	Node<Entry> * psau, *ptruoc;
    	if (position < 0 || position > count)
    	{
    		cout<<"Vi tri tim khong hop le!";
    		return errorrange;
    	}
    
    	if (position == 0){
    		item = pchinh->entry;
    		cout<<item<<endl;
    		return sucess;
    	}
    
    	else
    	{
    		SetPosition(position);
    		psau = pcurrent;
    	}
    	item = psau->entry;
    	cout<<item<<endl;
    	return sucess;
    }
    
    template <class Entry>
    void List<Entry>::SetPosition(int position) const{
    	if (position < CurrentPosition)
    	{
    		CurrentPosition = 0;	
    		pcurrent = pchinh ;
    	}
    	for(;CurrentPosition != position;CurrentPosition++)
    		pcurrent = pcurrent -> next;
    }
    
    template <class Entry>
    int List<Entry>::Size()const {
    	return count;
    }
    
    template <class Entry>
    bool List<Entry>::Empty() const {
    	if (count == 0)
    		return true;
    	else
    	return true;	
    }
    
    template <class Entry>
    bool List<Entry>::Full() const{
      NodeList<Entry> * location;
      try
      {
        location = new NodeList<Entry>;
        delete location;
        return false;
      }
      catch(std::bad_alloc exception)
      {
        return true;
      }
    }
    
    template <class Entry>
    void List<Entry>::Clear() const {
    	int i;
    	if (count ==0)
    		cout<<"Danh sach da rong!";
    	else
    	while (count!= 0) 
    		Remove(i);  //inform error at here : 'List<Entry>::Remove' : cannot convert 'this' pointer from     'const List<Entry>' to 'List<Entry> &'
    			
    }
    
    template <class Entry>
    void List<Entry>::Traverse(void (*duyet)(Entry & item)){
    	Node<Entry> * ptam= pchinh;
    	for (int i =0;i<count;i++)
    	{
    		(*duyet)(ptam->entry);
    		ptam = ptam->next;
    	}
    }
    
    #endif
    "UseList.cpp"

    Code:
    // DanhSach.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "List.cpp"
    #include "NodeList.cpp"
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    int main(){
    	List<int> list1;
    	List<int> list2;
    	int i;
    	list1.Insert(2);
    	list1.Insert(4,1);
    	list1.Insert(7,2);
    	list1.Insert(12,3);
    	list1.Insert(14,4);
    	list1.Insert(9,5);
    	list1.Insert(15,3);
    	list1.Insert(18,1);
    	list1.Insert(27);
    	list1.Remove(i);
    	cout<<i<<endl;
    	list1.Remove(i,3);
    	cout<<i<<endl;
    	list1.Remove(i,6);
    	cout<<i<<endl;
    	list1.Clear(); ->has Error at here
    	
    	list1.Replace(5);
    	list1.Replace(9,3);
    	list2 = list1;*/
    	getch();
            return 0;
    }
    Last edited by zaracattle; 10-09-2006 at 05:35 AM.

  2. #2
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Remove() isn't declared as a const method, which makes it illegal for Clear() (which is declared as const) to call it.
    My advice would be to completely remove the const-ness of Clear()... it doesn't make any sense in the first place. =S

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    52
    thanks for helping .....Osaou...
    Please tell me more about this error..Why I can't call Remove() if Clear() is declared as const....
    Last edited by zaracattle; 10-09-2006 at 08:22 AM.

  4. #4
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Objects that are declared as const can only call methods that are declared const as well.
    So, objects of your List class that are const...
    Code:
    const List myList;
    ...can for example call Empty() or Full(), because those methods are declared const, but they can Not call Insert()...

    You usually declare the methods that aren't changing anything about an object's state as const, if any.
    So, your Clear() method should Not, in my opinion, be declared const, since it presumably empties the list - which is a pretty radical change of the contained data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  2. Linked List
    By silicon in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2004, 07:47 PM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. Clear Function on Linked List
    By s0ul2squeeze in forum C++ Programming
    Replies: 1
    Last Post: 04-04-2002, 07:13 AM