cannot convert parameter 1 from 'int *' to 'int &'

lynnideal 2007-09-19 06:22:26
int test1(int &e)
{
e=10;
return 1;
}

void main()
{
int *e;
int k=test1(e); //这句出错,我的文件是.cpp
*e=20;
// if(test(e)==1)
if(k)
printf("%d\n",e);
}

cannot convert parameter 1 from 'int *' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue


如果我的文件改为.c来编译会出错
int test1(int &e) //这句出错,在.c的情况下
//error C2143: syntax error : missing ')' before '&'
//error C2143: syntax error : missing '{' before '&'
//error C2059: syntax error : '&'
//error C2059: syntax error : ')'
{
e=10;
return 1;
}

void main()
{
int *e=20;
int k=test1(e);//这句提示warning C4013: 'test1' undefined; assuming extern returning int

if(k)
printf("%d\n",e);
}
...全文
1419 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hs205118hs205118 2012-04-17
  • 打赏
  • 举报
回复
C没有引用
myhaikuotiankong 2011-10-14
  • 打赏
  • 举报
回复
如果是想用地址来传递的话
把int test1(int &e) 中的
&改成*就好了。。。
nana_aoe_2013 2011-10-14
  • 打赏
  • 举报
回复
这个*还真是没学透。我也发现了这个问题,学习了
beyondjjw 2007-09-19
  • 打赏
  • 举报
回复 1
引用传递参数,调用方式和按值调用的形式上是一样的
不能将指针作为引用传参数
xlfddlfd(楼主请点我加分^_^不用客气)
正解
arong1234 2007-09-19
  • 打赏
  • 举报
回复
把指针传给引用?这两者不一样,不能互相传
xlfddlfd 2007-09-19
  • 打赏
  • 举报
回复
int k=test1(e);
改为
test1(*e);
lynnideal 2007-09-19
  • 打赏
  • 举报
回复
我这里是分两种情况编译,上面那个是.cpp,下面那个是.c

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧