Re: initial value of reference to non-const must be an lvalue
Ron Natalie wrote:
asm23 wrote:
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.lang.c++/browse_thread/thread/e81cd9d9c2200d74/48c0774eeb3bd998?hl=en&lnk=gst&q=initial+value+of+reference+to+non-const#48c0774eeb3bd998
//*********************************************************************
// CODE
//*********************************************************************
#include <iostream.h>
class ostream; //Need for Overloading <<
Most likely spurious
class complex {
Since you're using some nonstandard compiler (as a result of iostream.h)
I'd be real careful about using type names that are defined in the
standard headers casually. Your compiler obviously doesn't have a
proper clue about namespaces.
Thanks for your reply. And Thanks for pointing out some mistakes in my code.
Now, I have change the whole code to standard type, which I use
namespaces. ALL works very well And NO "initial value ....." warnings
any more.
//------------------------CODE----------------------------------
#include <iostream>
using namespace std;
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
complex c=a+b;
cout << c << endl; //Print sum
int a1=2;
int b1=3;
cout<<a1+b1 <<endl;
return 0;
}
//----------------------------------------------------------------