-
August 10th, 2005, 04:07 AM
#1
error C2106: '=' : left operand must be l-value
Hi,
I get error (error C2106: '=' : left operand must be l-value) for
a>10?b=100:b=200;
but, not for
a>10?b=100b=200);
Please explain.
Regards
Sunnypriya
-
August 10th, 2005, 04:10 AM
#2
Re: error C2106: '=' : left operand must be l-value
I could see a funny character
I get error for
HTML Code:
a>10?b=100:b=200;
but not for
HTML Code:
a>10?b=100:(b=200);
-
August 10th, 2005, 04:31 AM
#3
Re: error C2106: '=' : left operand must be l-value
I did not get any errors with VC++ 6.0. What compiler are you using? how are a and b defined (although this doesnt make any difference because you say using the brackets doesnt cause this error). May be there is error somewhere else and you are getting the error for that statement in this one.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
August 10th, 2005, 04:38 AM
#4
Re: error C2106: '=' : left operand must be l-value
Thats true,
I am also using VC++ 6.0.
If I write a C program (save it as .c file), then I will get error.
If I use C++ (save it as .cpp file,), I dont get any error.
By the way, the portion of code is.
HTML Code:
int a,b;
a=5;
a>10?b=100:b=200;
.
But still, why it gives l value error in C.
-
August 10th, 2005, 05:03 AM
#5
Re: error C2106: '=' : left operand must be l-value
I suppose that operator precedences are different in C, and operator= must have a lower precedence than operator?:
So:
Is interpreted as:
Code:
(a>10?b=100:b)=200; // (a>10?b=100:b) is a r-value of type integer.
I think that C++ should also emit this error.
-
August 10th, 2005, 05:06 AM
#6
Re: error C2106: '=' : left operand must be l-value
I tried with borland C++ 5.5, and the result is the same:
operator= seems to have a higher precedence than operator?: in C++, and not in C.
-
August 10th, 2005, 05:08 AM
#7
Re: error C2106: '=' : left operand must be l-value
the expression you probably want though is
Code:
b = a >10 ? 100 : 200;
which has the correct operator precedence (>, ?:, = )
-
August 10th, 2005, 05:20 AM
#8
Re: error C2106: '=' : left operand must be l-value
may be yes it is the operator precendence that is causing this error but in C++ even its the same but its working there. I am not sure but this is what I got at this link. I would quote it as:
The conditional operator does not produce an lvalue. Therefore, a statement such as a ? x : y = 10 is not valid.
I will try finding some more information over it though and let you know if I find something but this ensures that l-value error but the reason for the one with braces working is still mystery for me. It seems quite logical that the operator precedence is the cause (as pointed out by SuperKoko).
Last edited by exterminator; August 10th, 2005 at 05:25 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
August 10th, 2005, 05:32 AM
#9
Re: error C2106: '=' : left operand must be l-value
Originally Posted by SunnyPriya
Hi,
I get error (error C2106: '=' : left operand must be l-value) for
a>10?b=100:b=200;
but, not for
a>10?b=100 b=200);
Please explain.
Regards
Sunnypriya
Hi,
I am using Visual Studio.NET 2003 for VC++ and I am not getting any error!
Rgs,
SoniSharad
-
August 10th, 2005, 05:44 AM
#10
Re: error C2106: '=' : left operand must be l-value
Originally Posted by exterminator
I will try finding some more information over it though and let you know if I find something but this ensures that l-value error but the reason for the one with braces working is still mystery for me. It seems quite logical that the operator precedence is the cause (as pointed out by SuperKoko).
This one
Code:
a>10?b=100:(b=200);
works well.
operator= can be used almost everywhere, as a normal operator: it assigns a value to its left operand which must be a l-value (that is b in this example), and returns the assigned value.
So, a>10?b=100b=200), evaluates to 100 or 200 depending on the value of a, after having assigned this value to b.
It is preferable, and clearer to use:
The fact that operator:? returns a r-value, means that:
Code:
int x=40,y=40;
int a=8;
(a>10?x:y)=42; // is invalid (if i refer to that rule).
but this code is accepted by bcc32 and gcc, because they are smart enough to understand that since, both x and y are l-value, the result of (a>10?x:y) is a l-value.
-
August 10th, 2005, 05:55 AM
#11
Re: error C2106: '=' : left operand must be l-value
Moreover:
Is valid for the ISO standard.
http://library.n0i.net/programming/c/cp-iso/expr.html
Originally Posted by 5.16-4-
-4- If the second and third operands are lvalues and have the same type, the result is of that type and is an lvalue.
-
August 10th, 2005, 06:01 AM
#12
Re: error C2106: '=' : left operand must be l-value
I finally understood the problem.
In fact, for both, C++ and C, the operator precedence is identical, and:
Code:
a>10?b=100:b=200; // is equivalent to (a>10?b=100:b)=200;
But with the ISO C++, (a>10?b=100:b) evaluates to a l-value, because the second and third operand are both l-values.
With ANSI C, (a>10?b=100:b) evaluates to a r-value, that is why, the assignment emit a compilation error!
-
August 10th, 2005, 06:49 AM
#13
Re: error C2106: '=' : left operand must be l-value
Bang!!! You got it SuperKoko. It surely somehow pointed that there was something to do with the standards. Great work.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|