473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

Jan 11 '08 #1
13 18882
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)

Jan 11 '08 #2
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.
Jan 11 '08 #3
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)

Jan 11 '08 #4
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".
Jan 11 '08 #5
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
Jan 11 '08 #6
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
Jan 11 '08 #7
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


Jan 11 '08 #8
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.
Jan 11 '08 #9
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.

Jan 11 '08 #10
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.

Jan 11 '08 #11
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)

Jan 11 '08 #12
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;
}
//----------------------------------------------------------------
Jan 12 '08 #13
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;
}
//----------------------------------------------------------------
Jan 12 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
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?
15
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...
9
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...
5
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)...
8
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 ". ...
5
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...
19
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() {
4
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...
27
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...
275
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'
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.