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;
}