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 18816
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: |
last post by:
If I need to check if a certain value does exist in a field, and return
either "yes" or "not" which query would be the most effestive?
|
by: Robert Mark Bram |
last post by:
Hi All!
I have the following code in an asp page whose language tag is:
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
// Find request variables.
var edition = Request.Form ("edition");
var...
|
by: ckerns |
last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0).
If value is null or non-numeric I want to assign the value of "0".
rowString = "L411" //conrol name
if (isNaN(eval...
|
by: Neal Coombes |
last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for
better from the unmoderated groups:
-------- Original Message --------
Subject: Return appropriately by value, (smart)...
|
by: Lyn |
last post by:
Hi,
Can anyone tell me how the initial value displayed in Combo Box is
determined when a form is opened?
I am loading the dropdown from one field ("CategoryName") of a table, with
"ORDER BY ". ...
|
by: Javier Campos |
last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-)
I can reformat a non-HTML post if you want me to (and...
|
by: daniel |
last post by:
This is a pretty basic-level question, but I'd really like to know, so
thanks for any help or pointers you can provide (like what I would
google for ;o)
Suppose:
<code>
myFunc()
{
|
by: Keith |
last post by:
Hello - this started out as a minor annoyance - and now is
starting to bother me more and more - I'm hoping someone
can help me.
I would like to have a combobox display - NOT initially be
blank...
|
by: Madhav |
last post by:
Hi all,
I did not understand why do the global vars are
initialized to NULL where as the block level variables have random
values? I know that the C standard requires this as was mentioned in a...
|
by: Astley Le Jasper |
last post by:
Sorry for the numpty question ...
How do you find the reference name of an object?
So if i have this
bob = modulename.objectname()
how do i find that the name is 'bob'
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |