cannot convert parameter 1 from 'const int' to 'int &',如何改正
今天实现线性表的程序中碰到这样的问题(其实以前也有碰到过,但是没花太多时间考虑)程序如下
//seqlist.h
#ifndef SEQLIST_H_
#define SEQLIST_H_
const int DefaultSize=10;
typedef int Item;
class SeqList
{
private:
int MaxSize;
int last;
Item *data;
public:
SeqList(int sz=DefaultSize);
~SeqList(){delete []data;}
int Length(){return last+1;} //返回线性表的实际长度
int Find(Item & x)const; //在线性表中查找元素X,查找成功返回其下标,否则返回-1
int IsIn(Item & x)const; //判断x是否在线性表中
bool IsEmpty() const; //判断线性表是否为空
bool IsFull() const; //判断线性表是否已满
Item Get(int i); //取得线性表中第i个元素的值
bool InsterElem(Item & x,int i); //在线性表中第i个元素插入x
bool DeleteElem(Item & x); //从线性表中删除元素x
int PriorElem(Item & x); //返回线性表中元素x的前驱元素
int NextElem(Item & x); //返回线性表中元素x的后继元素
};
#endif
//seqlist.cpp
#include "SeqList.h"
#include <iostream>
#include <cstdlib>
using namespace std;
SeqList::SeqList(int sz/* =DefaultSize */)
{
MaxSize=sz;
last=0;
data=new Item[MaxSize];
}
int SeqList::Find(Item & x)const
{
int i=0;
while(i<=last&&data[i]!=x)
i++;
if(i>last)
return -1;
else
return i;
}
int SeqList::IsIn(Item & x)const
{
int i=0;
int found=0;
while(i<=last&&!found)
if(data[i]!=x)
i++;
return found;
}
bool SeqList::IsEmpty()const
{
return last==0;
}
bool SeqList::IsFull()const
{
return last==MaxSize-1;
}
bool SeqList::InsterElem(Item & x,int i)
{
if(i<0||i>last||last==MaxSize-1)
return false;
else
{
last++;
for(int j=last;j>i;j--)
data[j]=data[j-1];
data[i]=x;
return true;
}
}
bool SeqList::DeleteElem(Item & x)
{
int i=Find(x);
if(i>=0)
{
last--;
for(int j=i;j<=last;j++)
data[j]=data[j+1];
return true;
}
return false;
}
int SeqList::PriorElem(Item & x)
{
int i=Find(x);
if(i>0 && i<=last)
return i-1;
else return -1;
}
int SeqList::NextElem(Item & x)
{
int i=Find(x);
if(i>=0 && i<last)
return i+1;
else
return -1;
}
Item SeqList::Get(int i)
{
if(i<0||i>last)
return -1;
else
return data[i];
}
//test.cpp
#include <iostream>
#include "SeqList.h"
using std::cout;
using std::endl;
int main()
{
Item a[10]={1,2,3,4,5,6,7,8,9,10};
SeqList b;
if(b.IsEmpty())
cout<<"线性表为空"<<endl;
int len=b.Length();
for(int i=0;i<len;i++)
{
if(!b.IsFull())
b.InsterElem(a[i],i);
}
for(i=0;i<len;i++)
cout<<b.Get(i)<<endl;
if(b.DeleteElem(7))
cout<<"Delete 7 is sucess "<<endl;
if(b.DeleteElem(14))
cout<<"Delete 14 is failed"<<endl;
for(i=5;i<12;i++)
{
if(b.IsIn(i))
cout<<"The element "<< i << "is in the List \n";
else
cout<<"The element "<< i << "is not in the List\n";
}
int j1=b.PriorElem(7);
cout<<b.Get(j1)<<endl;
int j2=b.NextElem(7);
cout<<b.Get(j2)<<endl;
return 0;
}
编译的时候出现问题如下
Compiling...
Test.cpp
E:\C++程序\数据结构练习\线性表\Test.cpp(19) : error C2664: 'DeleteElem' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
E:\C++程序\数据结构练习\线性表\Test.cpp(21) : error C2664: 'DeleteElem' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
E:\C++程序\数据结构练习\线性表\Test.cpp(31) : error C2664: 'PriorElem' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
E:\C++程序\数据结构练习\线性表\Test.cpp(33) : error C2664: 'NextElem' : cannot convert parameter 1 from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.
Test.obj - 4 error(s), 0 warning(s)
看起来问题出现的模式一样,应该怎么改呢。