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 13 18310
On 2008-01-10 20:28:57 -0500, asm23 <as********@gmail.comsaid:
>
cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum
Get rid of the noise:
cout << a+b;
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 wrote:
On 2008-01-10 20:28:57 -0500, asm23 <as********@gmail.comsaid:
>> cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum
Get rid of the noise:
Thanks, you say I should use the "#pragma warning (disable:XXX) to get
rid of the warnings?
>
> 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:
Pete Becker wrote:
>On 2008-01-10 20:28:57 -0500, asm23 <as********@gmail.comsaid:
>>> cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum
Get rid of the noise:
Thanks, you say I should use the "#pragma warning (disable:XXX) to get
rid of the warnings?
No. Simplify the statement where the warning occurs.
>>
>> cout << a+b;
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 wrote:
On 2008-01-10 21:09:36 -0500, asm23 <as********@gmail.comsaid:
>Pete Becker wrote:
>>On 2008-01-10 20:28:57 -0500, asm23 <as********@gmail.comsaid:
cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum
Get rid of the noise:
Thanks, you say I should use the "#pragma warning (disable:XXX) to get rid of the warnings?
No. Simplify the statement where the warning occurs.
>>> cout << a+b;
Now if you get the same warning, you know where to look.
I still get the warning when I change the code to: cout<<a+b;
I have another code to show, that sometimes, the type of warning became
error.
//////////////////////////////code/////////////////////////////////
int f(){
return 1;
}
int &bf=f();
error: initial value of reference to non-const must be an lvalue
////////////////////////////////////////////////////////////////////
I know the error that the return from f() is not lvalue. So, we can't
initialize the "bf".
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....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
operator<< is accepting a non constant ostream& and a non constant 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
You are attempting to pass to operator<< a non constant ostream& and a non
constant *temporary* complex& The temporary is the problem. Any changes
the function made to the temporary would be lost. A very simple fix. Change
your operator<< to:
ostream& operator<<(ostream& os, const complex& cnum)
And your problem should go away.
You need to learn const correctness. A temporary rvalue can be passes as a
const. I'm not sure of all the limitations, but it seems this is one of
them. Your clue that this is a const correctness error is given in the
error itself:
"initial value of reference to ***non-const*** must be an lvalue".
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
--
Jim Langston ta*******@rocketmail.com
On Jan 11, 10:43*am, "Jim Langston" <tazmas...@rocketmail.comwrote:
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....ead/thread/e81...
//************************************************** *******************
// * * * * * * * * * * * * 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
operator<< is accepting a non constant ostream& and a non constant 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
You are attempting to pass to operator<< a non constant ostream& and a non
constant *temporary* complex& *The temporary is the problem. *Any changes
the function made to the temporary would be lost. *A very simple fix. Change
your operator<< to:
ostream& operator<<(ostream& os, const complex& cnum)
And your problem should go away.
You need to learn const correctness. *A temporary rvalue can be passes as a
const. *I'm not sure of all the limitations, but it seems this is one of
them. *Your clue that this is a const correctness error is given in the
error itself:
"initial value of reference to ***non-const*** must be an lvalue".
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
--
Jim Langston
tazmas...@rocketmail.com- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
I am not getting any warning message with above code, using Visual
Studio 6.0 Compiler, You can create temporary object to hold result of
a+b, than pass temporary object to operator<<.
c= a+b;
cout<<c;
Regards,
Sachin
Jim Langston 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....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
operator<< is accepting a non constant ostream& and a non constant 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
You are attempting to pass to operator<< a non constant ostream& and a non
constant *temporary* complex& The temporary is the problem. Any changes
the function made to the temporary would be lost. A very simple fix. Change
your operator<< to:
ostream& operator<<(ostream& os, const complex& cnum)
And your problem should go away.
Thanks, I fix the problems by adding "const". The sentence "A temporary
rvalue can be passes as a const" is very important and I will remember
this. Also, I should learn some stuff on "const" more carefully.
>
You need to learn const correctness. A temporary rvalue can be passes as a
const. I'm not sure of all the limitations, but it seems this is one of
them. Your clue that this is a const correctness error is given in the
error itself:
"initial value of reference to ***non-const*** must be an lvalue".
>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
Sachin wrote:
On Jan 11, 10:43 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>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....ead/thread/e81... //************************************************** ******************* // 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
operator<< is accepting a non constant ostream& and a non constant 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
You are attempting to pass to operator<< a non constant ostream& and a non constant *temporary* complex& The temporary is the problem. Any changes the function made to the temporary would be lost. A very simple fix. Change your operator<< to:
ostream& operator<<(ostream& os, const complex& cnum)
And your problem should go away.
You need to learn const correctness. A temporary rvalue can be passes as a const. I'm not sure of all the limitations, but it seems this is one of them. Your clue that this is a const correctness error is given in the error itself: "initial value of reference to ***non-const*** must be an lvalue".
>>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
-- Jim Langston tazmas...@rocketmail.com- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
I am not getting any warning message with above code, using Visual
Studio 6.0 Compiler, You can create temporary object to hold result of
a+b, than pass temporary object to operator<<.
c= a+b;
cout<<c;
Regards,
Sachin
Thanks, I'm using vc6, but I'm using the compiler supplied by Intel. So,
they have some differences. When I use the original compiler in VC6,
there's no warning even I set the warning level to 4, which is the
highest level.
On Jan 11, 9:08*am, asm23 <asmwarr...@gmail.comwrote:
Sachin wrote:
I am not getting any warning message with above code, using Visual
Studio 6.0 Compiler, You can create temporary object to hold result of
a+b, than pass temporary object to operator<<.
c= a+b;
cout<<c;
Thanks, I'm using vc6, but I'm using the compiler supplied by Intel. So,
they have some differences. When I use the original compiler in VC6,
there's no warning even I set the warning level to 4, which is the
highest level.- Hide quoted text -
To Sachin, c isn't a temporary object, you're confusing the issue.
To asm23, no surprise about VC6 and warning level 4 - read past
posts in this n.g and you'll soon be chucking VC6 in the bin.
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....c0774eeb3bd998
//************************************************** *******************
// 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.
On 2008-01-10 21:40:40 -0500, asm23 <as********@gmail.comsaid:
Pete Becker wrote:
>On 2008-01-10 21:09:36 -0500, asm23 <as********@gmail.comsaid:
>>Pete Becker wrote: On 2008-01-10 20:28:57 -0500, asm23 <as********@gmail.comsaid:
> cout <<"a=" << a <<",b=" << b <<" a+b = " << a+b << endl; //Print sum
Get rid of the noise: Thanks, you say I should use the "#pragma warning (disable:XXX) to get rid of the warnings?
No. Simplify the statement where the warning occurs.
>>>> cout << a+b;
Now if you get the same warning, you know where to look.
I still get the warning when I change the code to: cout<<a+b;
Now that you've isolated the problem, think about what that statement does.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
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....c0774eeb3bd998
//************************************************** ******************* // 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;
}
//----------------------------------------------------------------
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....c0774eeb3bd998
//************************************************** ******************* // 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;
}
//---------------------------------------------------------------- This discussion thread is closed Replies have been disabled for this discussion. Similar topics
20 posts
views
Thread by |
last post: by
|
15 posts
views
Thread by Robert Mark Bram |
last post: by
|
9 posts
views
Thread by ckerns |
last post: by
|
5 posts
views
Thread by Neal Coombes |
last post: by
|
8 posts
views
Thread by Lyn |
last post: by
|
5 posts
views
Thread by Javier Campos |
last post: by
|
19 posts
views
Thread by daniel |
last post: by
|
4 posts
views
Thread by Keith |
last post: by
|
27 posts
views
Thread by Madhav |
last post: by
|
275 posts
views
Thread by Astley Le Jasper |
last post: by
| | | | | | | | | | |