CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2002
    Location
    Mumbai
    Posts
    197

    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

  2. #2
    Join Date
    May 2002
    Location
    Mumbai
    Posts
    197

    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);

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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.

  4. #4
    Join Date
    May 2002
    Location
    Mumbai
    Posts
    197

    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.

  5. #5
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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:
    Code:
    a>10?b=100:b=200;
    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.

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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.

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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 (>, ?:, = )

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up 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.

  9. #9
    Join Date
    May 2003
    Location
    India
    Posts
    101

    Angry Re: error C2106: '=' : left operand must be l-value

    Quote 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=100b=200);

    Please explain.

    Regards
    Sunnypriya

    Hi,

    I am using Visual Studio.NET 2003 for VC++ and I am not getting any error!

    Rgs,
    SoniSharad

  10. #10
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: error C2106: '=' : left operand must be l-value

    Quote 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:
    Code:
    b=(a>10?100:200);
    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.

  11. #11
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: error C2106: '=' : left operand must be l-value

    Moreover:
    Code:
    (a>10?x:y)=42;
    Is valid for the ISO standard.
    http://library.n0i.net/programming/c/cp-iso/expr.html
    Quote 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.

  12. #12
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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!

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured