Thread: l value required

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    l value required

    Hi,

    I had some of the issues referred to "lvalue required" in c++, but i could not find for my specific case.
    I was trying to write a small code using "Type reference" concept.

    Below is my simple code:

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    
    int main()
    
    
    {
     clrscr();
     int y1=5,y3=6;
     int & y2 = y1;
    
    
     cout << "y2 is "<< y2 <<endl<<" address of y2: "<< &y2 <<"\n";
    
    
     &y2 = &y3;
    
    
     cout << "y2 is "<< y2 <<endl<<" address of y2: "<< &y2 <<"\n";
    
    
     getch();
     return 0;
    }
    
    


    Now i get the error saying "lvaue required".
    Could any one please help me on this?

    anil


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What did you intend by writing this line?
    Code:
    &y2 = &y3;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    >&y2 = &y3;
    This is wrong. The address isn't a lvalue.

    Also, you can not ..in any way, unset a reference..if that is what you were trying .

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Well.. i was trying to point the value of y3 to y2.

    (That is how it is shown in text book sample code)

    in the initial first statements, i have assigned the value of y1 to y2
    int & y2 = y1;

    and then i want to overwrite the values y1 by y3, which is assigned to y2
    &y2 = &y3;

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by anil_ View Post
    Well.. i was trying to point the value of y3 to y2.

    (That is how it is shown in text book sample code)

    in the initial first statements, i have assigned the value of y1 to y2
    int & y2 = y1;

    and then i want to overwrite the values y1 by y3, which is assigned to y2
    &y2 = &y3;
    Overwriting the value and trying to change the address, are totally different .
    You can NOT make y2 point to y3.
    (You could, if y2 was a pointer instead of a reference )

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anil_
    in the initial first statements, i have assigned the value of y1 to y2
    int & y2 = y1;
    No, you did not assign the value of y1 to y2. Rather, you made y2 an alias for y1.

    Quote Originally Posted by anil_
    and then i want to overwrite the values y1 by y3, which is assigned to y2
    &y2 = &y3;
    Since y2 is an alias of y1, you should write:
    Code:
    y2 = y3;
    Quote Originally Posted by anil_
    i was trying to point the value of y3 to y2.
    It sounds more like you were trying to set the value of y2 to the value of y3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    ok i got the point...
    Last edited by anil_; 10-06-2011 at 11:13 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anil_
    But now, how do i change the code so that the address of y2 and y3 are same.
    As manasij7479 has stated, you cannot rebind the reference. y2 is an alias for y1; it cannot become an alias for y3, in this context.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    y2 = y3;

    But for the above code, the address of y3 is different from y2.

    I need to change the address of y2 to be same as y3.
    what should i do then..?

    anil
    Last edited by anil_; 10-06-2011 at 11:50 AM.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Quote Originally Posted by laserlight View Post
    As manasij7479 has stated, you cannot rebind the reference. y2 is an alias for y1; it cannot become an alias for y3, in this context.
    i agree with manasij7479 that it is not possible trying to play with 3 variables point to 2 addresses at the same time. It should be only two on two.

    But in my case, y2 should be an alias to y1, initially

    and then y2 should become an alias to y3, finally

    anil
    Last edited by anil_; 10-06-2011 at 11:37 AM.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by anil_ View Post
    i agree with manasij7479 that it is not possible trying to play with 3 variables point to 2 addresses at the same time. It should be only two on two.

    But in my case, y2 should be an alias to y1, initially

    and then y2 should become an alias to y3, finally

    anil
    You misunderstood what I meant.
    Once you 'set' a reference as an alias for a variable, you can not change it to 'point' to another variable .

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    ok, i think the text book is wrong.

    In the book, he talks of changing the reference as an alias for another variable.

    Any how i thank you all for this nice conversation we had. it was such a nice KT among each other.

    Once again thank you very very much.

    anil
    Last edited by anil_; 10-06-2011 at 11:54 AM.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    But in my case, y2 should be an alias to y1, initially

    and then y2 should become an alias to y3, finally
    In the book, he talks of changing the reference as an alias for another variable.
    Just to be completely clear, that is strictly prevented by the language.

    Could you tell us which book tries to incorrectly suggest that it can be done please?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Quote Originally Posted by iMalc View Post
    Could you tell us which book tries to incorrectly suggest that it can be done please?
    "Object Oriented Programming with C++ : M.P.Bhave and S.A. Patekar"

    what confuses me is, there is an output text window with all the results. I have mailed to author asking for further clarifications.

    Well any how i have moved ahead reading other topics.

    anil

  15. #15
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Not being rude...but please get a better book. (Look here for some good recommendations)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help required....plz
    By ruku in forum Tech Board
    Replies: 6
    Last Post: 07-24-2006, 09:28 PM
  2. Help required
    By ItsMeHere in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 08:26 AM
  3. Little help required...
    By Ninestar in forum C Programming
    Replies: 5
    Last Post: 03-17-2006, 05:32 PM
  4. Help Required!!!!!
    By bobthebullet990 in forum C Programming
    Replies: 14
    Last Post: 11-27-2005, 03:56 PM
  5. little help required
    By Lynux-Penguin in forum C Programming
    Replies: 4
    Last Post: 04-27-2002, 01:34 PM