Skip to content

Lvalue Required As Left Operand Of Assignment Avr Studio 6

  1. Registered User
    Join Date
    Feb 2010
    Posts
    58

    lvalue required as left operand of assignment

    i get this error message for each of these lines when i try to run the program. i have no idea what it means. what am i doing wrong? there is a data file elsewhere in the program, and i'm reading info from that, and one column is numbers from 0-6. each of those corresponds to a name, which is what this function is supposed to do, equate the number to the family name. here's the portion of the code that's getting error
    string familyName( int familyCode ) { int n; inFile>>n; switch (n) { case 0: 0=Drake; break; case 1: 1=Spencer; break; case 2: 2=Morgan; break; case 3: 3=Corinthos; break; case 4: 4=Webber; break; case 5: 5=Quatermaine; break; case 6: 6=Davis; break; } }

  2. and the Hat of Guessing
    Join Date
    Nov 2007
    Posts
    14,185
    = is not symmetric in C++. The value on the right is placed in the variable on the left. Since 0, 1, ..., 6 are not variables, but constants, the assignment cannot happen.

  3. Registered User
    Join Date
    Feb 2010
    Posts
    58
    so i'd have to make each of the numbers variables in order for it to work? but that's not possible, is it?

  4. and the Hat of Guessing
    Join Date
    Nov 2007
    Posts
    14,185
    Last edited by tabstop; 03-26-2010 at 05:01 PM.
    The problem here is your belief, which I'm not sure where it came from, that somehow you want the number 0 to be assigned the value Drake. That's not what your English text at the top of your original post says you want to do, and it's not what anybody thinking about what your problem would want to do.

    (Hint: Your English text says that, for example, if familyCode is 0 you want some string to be "Drake".

  5. Registered User
    Join Date
    Apr 2006
    Posts
    2,146
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.
    "equate" does not mean assign. It means create a logical mapping.

  6. Registered User
    Join Date
    Feb 2010
    Posts
    58
    bear with me here, i'm not knowledgeable at all in c++. i appreciate the help so far. so would declaring the family names as int solve the problem?


  7. Registered User
    Join Date
    Feb 2010
    Posts
    58
    ok, how do i set a number equal to the name? i need this program to read 0 from the text file and display drake. i have no idea whatsoever how to do this, i've declared them as strings and ints, i don't know what to do.


  8. Registered User
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    4
    When you use the = operator, it takes the value on the right, and assigns it to a variable on the left. A value can be a number, a letter, a string of text, or another variable among other things.

    This statement:

    Won't work because '0' is not a variable (which is causing the "lvalue required as left operand of assignment" error). Another thing to remember is that a string must be put in "quotes", so unless Drake is a variable, that would also be a problem.

    Now, if I understand what you're trying to do, this should fix your problem:

    string familyName( int familyCode ) { int n; inFile>>n; switch (n) { case 0: cout<<"Drake"; break; case 1: cout<<"Spencer"; break; case 2: cout<<"Morgan"; break; case 3: cout<<"Corinthos"; break; case 4: cout<<"Webber"; break; case 5: cout<<"Quatermaine"; break; case 6: cout<<"Davis"; break; } }
    I'm new to coding myself, so lemme know if I got something wrong, but this should help.

  9. Registered User
    Join Date
    Feb 2010
    Posts
    58

    oh christ, cout<<... why on earth did i not think of that. thank you much, kind sir. thanks to everyone else as well. it's been a long day , ha

  10. Registered User
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    4



'L-value' and 'R-Value' describe attributes of the Left-hand side and Right-hand side, respectively, of an assignment. An R-value is a quantity of some kind (not necessarily numerical), usually the result of evaluating an expression, but also a constant, such as '5' or the string "Hello, World!\n". An L-value is a something that a program can assign an R-value to, such as a named variable, or a raw memory or port address. This WikiPedia article has a more complete description.

"lvalue required as left operand of assignment" simply means that the left side of your assignment isn't an assignable address or convertible to one. PA0 is #defined as a simple constant without l-value properties.

You're on the right track - here is an Arduino article about ports and the I/O registers associated with them, and direct manipulation of those registers. Despite its caveats, direct port manipulation is a useful technique for reducing both program size and timing-skew in some situations.

These statements:

will both define an l-value you can assign to (but note: it's a whole port, not just a pin) and change its value to turn on pin-0. The RED_ON expression reads: read PORTA, OR that with '1' (so you won't change any other bits in the port); the assignment writes the new value back to the port.

(Do note that your code as written - even assuming the assignment statement was correct - will repetitively turn on the LED, which is no different from turning it on once).

answered Jun 18 '12 at 18:35