标题:cannot convert parameter 1 from 'const int' to 'int &',如何改正
只看楼主
heyyroup
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2006-6-14
 问题点数:0 回复次数:2 
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)
看起来问题出现的模式一样,应该怎么改呢。
搜索更多相关主题的帖子: parameter convert int cannot const 
2007-09-25 21:27
远去的列车
Rank: 1
等 级:新手上路
威 望:2
帖 子:205
专家分:0
注 册:2007-8-7
得分:0 
声明:bool DeleteElem(Item & x);
调用:
if(b.DeleteElem(7))
if(b.DeleteElem(14))

声明:int PriorElem(Item & x);
调用:int j1=b.PriorElem(7);

声明:int NextElem(Item & x);
调用:int j2=b.NextElem(7);

调用的时候全错了,自己改,很容易理解

[此贴子已经被作者于2007-9-27 9:23:49编辑过]


C++学习
2007-09-27 09:20
zoominla
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2007-9-13
得分:0 
函数中无需改变传入参数的值时,定义时把 “Item & x ” 都改成 "const Item & x"
2007-09-27 12:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-172866-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023736 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved