找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1883|回复: 0

cannot convert parameter 1 from 'const int' to 'int &',如何改正

[复制链接]
发表于 2009-11-3 01:34:29 | 显示全部楼层 |阅读模式 IP:江苏扬州
今天实现线性表的程序中碰到这样的问题(其实以前也有碰到过,但是没花太多时间考虑)
程序如下
//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)
看起来问题出现的模式一样,应该怎么改呢。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 05:25 , Processed in 0.284925 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表