c++问题下面这个程序为啥不能编译?

来源:百度知道 编辑:UC知道 时间:2024/05/08 12:22:37
#include<iostream.h>
void main()
{
const int num=500;
int& ref=num;
cout<<ref<<endl;
ref=ref+100;
cout<<ref<<endl;
cout<<num<<endl;
}
编译时出现:cannot convert from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue

去掉 & 我就没明白你加个 & 有啥用呢~ 呵呵 去掉就行了

把const去了没用,你要是不信你试试,把&去除掉就能编译了,你试试看!

常量与引用的类型不匹配,要么引用加个const,要么把num的const定义去掉

ref是num的引用(别名),你把num定义为常变量,就等于把ref也指定为常变量了,常变量是不能再对其赋值的.

把const取了