error non-lvalue in assignment

I am not certain what this is trying to tell me, or better put, how I am to fix it.

I am trying to write a function for a class. Since this is an assignment I would appreciate not getting code for an answer, but rather if someone can tell me where to look for the issue?

Here is the set function I am trying to write. The private data in this class is an array, Hand TheHand[3] and int numCards(the number of cards in TheHand) in the .h
1
2
  //set the number of cards in the hand
  void setNumCards(int value);


In the .cpp
1
2
3
4
5
//Action:  Sets numCards to value
//Returns: Nothing
  void Hand::setNumCards(int value)  {
  	getNumCards() = value;
  }


The error I am getting is this:
Hand.cpp: In member function `void Hand::setNumCards(int)':
Hand.cpp:68: error: non-lvalue in assignment
The non-lvalue part of your error is telling you, getNumCards() is a function, and not a left value. It should be the rvalue (right side of operator). Flip that statement around.
Last edited on
That fixed it. I wonder if it is because the value being passed in has to be the lvalue? Do you know why it fixed it?
Yes, the left value or 'value' variable in your case, has to be the one that is accepting the value of the right value, or function getNumCards() in your case.

The way you had it, you were trying to assign getNumCards() the value of the variable 'value'. This isn't how things work.

C++ - In assignment operator expressions ( where a = b), then lvalue must be populated with data from rvalue and functions cannot be assigned values this way.

L-Value's and R-Values: http://msdn.microsoft.com/en-us/library/f90831hc%28v=vs.80%29.aspx
Last edited on
Thank you so much for the explanation and the link. Going to go read it now.
I can't let this go...

The original poster badkaykay posted the following code:
1
2
3
4
5
//Action:  Sets numCards to value
//Returns: Nothing
  void Hand::setNumCards(int value)  {
  	getNumCards() = value;
  }


and got the non-lvalue in assignment error.

Then poster rcast said
rcast wrote:
The non-lvalue part of your error is telling you, getNumCards() is a function, and not a left value. It should be the rvalue (right side of operator). Flip that statement around.


But that will give us code that now looks like thsi
1
2
3
4
5
//Action:  Sets numCards to value
//Returns: Nothing
  void Hand::setNumCards(int value)  {
  	value = getNumCards();
  }


This is an even bigger error than the original problem - true it will
not now generate a Lvalue error because funtion parameters ARE LValues
BUT all we are doing now is changing the original function parameter value and we have achieved nothing at all!!!!!!


*of course - this only applies if I interpreted rcast answer correctly.
Last edited on
badkaykay wrote:
That fixed it.


??
I didn't see value was the parameter. Why would you want to accept a parameter and then assign it a separate value immediately? This doesn't make sense. I was just fixing his syntax, not his logic.

Im confused
Last edited on
Topic archived. No new replies allowed.