Thread: error C2105: '++' needs l-value

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    error C2105: '++' needs l-value

    I've compiled the following program in VC++ environment. I've got an error "error C2105: '++' needs l-value" in line 6. Why am I getting this error? What is l-value? Why am I not getting the same error in line 5?



    #include <iostream.h>
    void main()
    {
    int i=2;
    ++++i; --------line 5
    i++++; --------line 6
    cout<<i;
    }





    Thank You,

    Sincerely,
    Uday

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    ++++ Isn't a C++ operator. use ++.
    i++;
    ++i;

    an l-value is the value to the left of an expression; normally a variable name.

  3. #3
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    in case you wanted to increment it twice by doing that just use

    i+=2;

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    I think it has to do with binding.

    perhaps five evaluates as (++(++i)) and six evaluates as (i++(++))

    Just another reason why ++i is better than i++. Also, even if it worked, there would be a subtle logic error in that postfix, postincrement ++ returns the previous values of i and then increments, whereas the prefix, preincrement ++i returns the new value of i. If you use either as the only statement in a line, there is no problem, but if you try to use it in conjuntion with anything else, you get problems.

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Just another reason why ++i is better than i++.
    How can one be better then the other? Each has its own implementation..

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >++++i; --------line 5
    >i++++; --------line 6

    GCC gives errors at both lines, so I think it's compiler specific. I know that it shouldn't, but unfortunately, most compilers don't implement the standard.

    Since i is a variable and not an address, this error occurs.

Popular pages Recent additions subscribe to a feed