对非const的引用的初始值必须是左值 [英] initial value of reference to non-const must be an lvalue

查看:119
本文介绍了对非const的引用的初始值必须是左值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来澄清警告引用的初始值

到非const必须是左值。


我我正在这个小组中寻找某人有类似的情况,比如

我。我在帖子中找到了:
http://groups.google.com/group/comp....c0774eeb3bd998


// ************************************ ************** *******************

//代码

// ********************************************** **** *******************

#include< iostream.h>

class ostream; //需要重载<<

类复杂{

double re,im; //班级的私人成员

public:

complex(){re = 0.0; IM = 0.0; } //空构造函数

complex(double r,double i = 0.0)//来自2个双精度的构造函数

{re = r; IM = I; }

朋友ostream& operator<(&ostream&,complex&);

朋友内联复杂运算符+(复杂,复杂);

};

inline complex operator +(complex a1,complex a2)//添加2个复数

{return complex(a1.re + a2.re,a1.im + a2.im); }

ostream& operator<(ostream& os,complex& cnum)//输出复数

{os<< "("<< cnum.re<<<"""<<< cnum.im<<"")" ;;返回; };


int main(无效)

{complex a(1,2),b(3,4); //定义复数

cout<<" a =" << a<<",b =" << b<<" a + b = << a + b<< ENDL; //打印总和

}


我的编译器也收到相同的警告。

但是我添加了一些声明:


// ************************************ ************** *******************

//代码

// ********************************************** **** *******************


int a1 = 2;

int b1 = 3;

cout<< a1 + b1;


这很顺利。


什么'这两个代码之间的区别是什么?我如何摆脱这些

警告?


谢谢

Hi,I need some help to clarify the warning "initial value of reference
to non-const must be an lvalue".

I''m searching in this groups to find someone has the same situation like
me. I found in the Post:
http://groups.google.com/group/comp....c0774eeb3bd998

//************************************************** *******************
// CODE
//************************************************** *******************
#include <iostream.h>
class ostream; //Need for Overloading <<
class complex {
double re, im; //Private members of class
public:
complex() { re=0.0; im=0.0; } //Empty Constructor
complex(double r, double i=0.0) //Constructor from 2 doubles
{ re=r; im=i; }
friend ostream& operator<<(ostream&, complex&);
friend inline complex operator+(complex, complex);
};
inline complex operator+(complex a1, complex a2) //Add 2 complex numbers
{ return complex(a1.re+a2.re, a1.im+a2.im); }
ostream& operator<<(ostream& os, complex& cnum) //Output a complex number
{ os << "(" << cnum.re << "," << cnum.im << ") "; return os; };

int main(void)
{ complex a(1,2), b(3,4); //Define complex numbers
cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum
}

I also get the same warning in my compiler.
but I add some statement like:

//************************************************** *******************
// CODE
//************************************************** *******************

int a1=2;
int b1=3;
cout<<a1+b1;

this goes very well.

What''s the difference between these two code? How can I get rid of these
warnings?

Thanks

推荐答案

On 2008-01-10 20:28:57 -0500,asm23< as ******** @ gmail.comsaid:
On 2008-01-10 20:28:57 -0500, asm23 <as********@gmail.comsaid:

>

cout<<" a =" << a<<",b =" << b<<" a + b = << a + b<< ENDL; //打印金额
>
cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum



摆脱噪音:

Get rid of the noise:


cout<< A + B;
cout << a+b;



现在如果你得到同样的警告,你知道在哪里看。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者

标准C ++库扩展:教程和参考
www.petebecker.com/tr1book )

Now if you get the same warning, you know where to look.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


Pete Becker写道:
Pete Becker wrote:

On 2008-01-10 20:28:57 -0500,asm23< as ******** @ gmail.comsaid:
On 2008-01-10 20:28:57 -0500, asm23 <as********@gmail.comsaid:

>>
cout<<" a =" << a<<",b =" << b<<" a + b = << a + b<< ENDL; //打印金额
>>
cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum



摆脱噪音:


Get rid of the noise:



谢谢,你说我应该使用#pragma warning(disable:XXX)获取

摆脱警告?

Thanks, you say I should use the "#pragma warning (disable:XXX) to get
rid of the warnings?


>
>

> cout<< A + B;
> cout << a+b;



现在,如果你收到相同的警告,你知道在哪里看。


Now if you get the same warning, you know where to look.


On 2008-01-10 21:09:36 -0500,asm23< as ******** @ gmail.comsaid:
On 2008-01-10 21:09:36 -0500, asm23 <as********@gmail.comsaid:

Pete Becker写道:
Pete Becker wrote:

> On 2008-01-10 20:28:57 -0500,asm23< as ******** @ gmail.comsaid:
>On 2008-01-10 20:28:57 -0500, asm23 <as********@gmail.comsaid:

>>>
cout<<" a =" << a<<",b =" << b<<" a + b = << a + b<< ENDL; //打印金额
>>>
cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum


摆脱噪音:


Get rid of the noise:



谢谢,你说我应该使用#pragma警告(禁用:XXX)获取

摆脱警告?

Thanks, you say I should use the "#pragma warning (disable:XXX) to get
rid of the warnings?



No.简化发生警告的声明。

No. Simplify the statement where the warning occurs.


>>
>>

>> cout<< A + B;
>> cout << a+b;


现在,如果你收到相同的警告,你知道在哪里看。


Now if you get the same warning, you know where to look.



-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者

标准C ++库扩展:a教程和参考

www.petebecker.com/tr1book


--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于对非const的引用的初始值必须是左值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆