Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

...must be lavalue warning

62 views
Skip to first unread message

Thelma Lubkin

unread,
Dec 6, 1998, 3:00:00 AM12/6/98
to
What can I do to get rid of this warning ---

warning: initial value of reference to non-const must be an lvalue?

I include a very short piece of code that demonstrates the problem.
I want to do arithmetic in a formulaic manner, w/o having to provide
an lvalue for each intermediate calculation: I have lots of code that
generates such warnings but works fine; I'd rather not have pages of
warnings for every program run and the fear that a 'warning' will
someday turn on me and cause problems.
--thanks
thelma

//*********************************************************************
// 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
}
//*********************************************************************
// MAKEFILE
//*********************************************************************
ctest.cc -o ctest

//*********************************************************************
// WARNING on RUNNING MAKEFILE
//*********************************************************************
Script started on Sun Dec 6 14:02:02 1998329
/u/thelma/eliwork/longtrain/Robin/Robin_clean> makctest
"ctest.cc", line 24: warning: initial value of reference to non-const must be an lvalue
cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum
^
//*********************************************************************
// OUTPUT of RUN
//*********************************************************************
a=(1,2) ,b=(3,4) a+b = (4,6)
331 /u/thelma/eliwork/longtrain/Robin/Robin_clean> exit
script done on Sun Dec 6 14:02:13 1998


Biju Thomas

unread,
Dec 6, 1998, 3:00:00 AM12/6/98
to
Thelma Lubkin wrote:
>
> What can I do to get rid of this warning ---
>
> warning: initial value of reference to non-const must be an lvalue?
>

This warning means that you are taking a non-const reference to a
temporary variable. This temporary is going to die shortly, and, after
that, the reference will be invalid. For example, the following is
illegal:

int a = 0, b = 0;
int& i = a + b;

For standard conforming compilers, this should give an error.

> I want to do arithmetic in a formulaic manner, w/o having to provide
> an lvalue for each intermediate calculation: I have lots of code that
> generates such warnings but works fine;

It may work in case if you are not using the reference after the death
of the temporary. But, don't depend upon this construct.

> I'd rather not have pages of
> warnings for every program run and the fear that a 'warning' will
> someday turn on me and cause problems.

It will surely cause problems some day.

> friend ostream& operator<<(ostream&, complex&);

> ostream& operator<<(ostream& os, complex& cnum) //Output a complex number

In this function, the last parameter should be 'const complex&'. This
will make the warning go away.


> "ctest.cc", line 24: warning: initial value of reference to non-const must be an lvalue
> cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum
> ^

The warning comes becase (a+b) is a temporary. Once you make the change
suggested above, it should work, since it is correct to bind a const
reference to a temporary.

Regards,
Biju Thomas

0 new messages