A
asm23
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...ue+of+reference+to+non-const#48c0774eeb3bd998
//*********************************************************************
// 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
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...ue+of+reference+to+non-const#48c0774eeb3bd998
//*********************************************************************
// 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