Thread: invalid lvalue in assignment

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    invalid lvalue in assignment

    hi. I keep getting the invalid lvalue in assignment for the following code

    Code:
    void rbnode::rotate_left(rbnode *x) {
    	printf("rotate left!\n");
    	rbnode *y = new rbnode(0, Red, NULL, NULL, NULL);
    	&y = &x;
    	x->right = y->left;
    	if(y->left != NULL){
    		y->left->parent = x;
    	}
    	y->parent = x->parent;
    
    	if(x == x->parent->left){
    		x->parent->left = y;
    	}
    	else{
    		x->parent->right = y;
    	}
    
    	y->left = x;
    	x->parent = y;
    
      
    } // rbnode::rotate_left
    The error state that &y = &x has error. am I doing something wrong here? I search on google about the reason of getting this error but none of them apply to me here.

    thx for the help

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You're using '&' to take the address of y. The resulting value is not an l-value, so the assignment is improper.

    I'm not sure what you WANT to accomplish... I assume you're trying to copy the object pointed to by x into the object pointed to by y. In that case, you need to use '*' instead of '&':

    Code:
    *y = *x;
    And yet I have doubts that this is what you want, because if it was, surely you would have just invoked the copy constructor when you call new? Confused.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    rbnode *y = new rbnode(0, Red, NULL, NULL, NULL);
    &y = &x;
    y is a pointer, &y is the address of the pointer variable on the stack. Obviously you can't assign a new address to this variable (it's fixed when you call the function). If you want y to point to x, they are both pointers so you just assign one to another:
    Code:
    y = x;
    However, this constitutes a memory leak as you've just lost what you assigned to y in the previous line of code during the call to new. Are you sure you need to be doing this?

    Echo confused as to what you're trying to do there.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    thx for the quick reespose.

    All I want to do here is to copy x into y. The reason that I didn't pass in all the parameter when I new the object is I keep getting segmentation fault error. So I was thinking that this way might help me for a bit.

    anyway, I have tried using *y = *x but now the segmentation error came back....What really happen that it gives me segmentation error anyway?Is ther a way to fix it?

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    Code:
    rbnode *y = new rbnode(x->key, x->colour, x->left, x->right, x->parent);
    this was whwat I originally did but segmentation fault occur..

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Does rbnode have a valid copy constructor and copy assignment operator? What does that class declaration look like?

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    Code:
    // Constructor
    rbnode::rbnode(int s_key, rbcolour s_colour, 
    			   rbnode *s_left, rbnode *s_right, rbnode *s_parent) {
      key = s_key;
      colour = s_colour;
      left = s_left;
      right = s_right;
      parent = s_parent;
    } // rbnode::rbnode
    rbnode only has this constructor.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you have allocated resources in your class? All those pointers were allocated with new? Where are they deleted?

    Chances are you have some other problems in your code that cause the segmentation fault. I doubt that using a copy constructor would be any different than doing what you tried to do above. I would use this:
    Code:
    rbnode *y = new rbnode(*x);
    to create a new copy of another node, at least until you figure out what's causing your segmentation fault.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by saipkjai View Post
    Code:
    // Constructor
    rbnode::rbnode(int s_key, rbcolour s_colour, 
    			   rbnode *s_left, rbnode *s_right, rbnode *s_parent) {
      key = s_key;
      colour = s_colour;
      left = s_left;
      right = s_right;
      parent = s_parent;
    } // rbnode::rbnode
    rbnode only has this constructor.
    It also has a default copy constructor. Since all your data seems to be held by value, the default copy constructor should work fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. :523: error: invalid lvalue in assignment
    By nasim751 in forum C Programming
    Replies: 10
    Last Post: 04-14-2008, 04:08 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. invalid lvalue in assignment
    By antonis in forum C Programming
    Replies: 8
    Last Post: 10-09-2005, 12:00 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM