我想测试引用的用法,敲了以下代码
int main()
{
/*List tou;
InitList(tou);
List shou;
InitList(shou);
*/
int &a=1;
int b=2;
int &c=b;
getch();
return 0;
}
编译后出现下面错误
--------------------Configuration: 线性表 - Win32 Debug--------------------
Compiling...
线性表.cpp
E:\数据结构\线性表.cpp(46) : error C2440: 'initializing' : cannot convert from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.
线性表.exe - 1 error(s), 0 warning(s)
我感觉非常奇怪的,首先这个转换错误出现一次,证明只有一次赋值出错。根据以往经验,我判断是&a=1出错。出错原因就是整型变量转化成引用整型,不合法。
&c=b也是整型变量转化成引用变量,为啥就合法了呢?