# Non-lvalue in assigmnet Error?

• 03-25-2007
warfang
Non-lvalue in assigmnet Error?
I have an object that creates a multi dimensional array.

Object a(10) // this creates an array: temp [10][2]

In the main function I need to access the object this way:

a[6] = 6 * 10 // I want it to set temp[6][2] to 60

I keep getting a "non-lvalue in assignment" error. I had to overload the [] operator and I'm returning a pointer to that position in the array.

I've searched google for "non-lvalue in assignment" but I wasn't able to work out my problem from that. If you can help that would be great.
• 03-25-2007
warfang
Also I would like to add that I have to use the syntax:

a[6] = 6 * 10;

So the problem is in my Class.
• 03-25-2007
kantze
Why don't you post some of your code to make your question clearer. Its hard to understand what you want.

And , as I know , lvalue == left value. Which is the left part of an assignment operation. So if we say x=y , x is an lvalue.
Here is an lvalue error:
Code:

```const int x=3, y=5; x = y;```
this is wrong because we can't assign to const. So a const variable can't be an lvalue...
• 03-25-2007
tomcant
Quote:

a[6] = 6 * 10 // I want it to set temp[6][2] to 60
The element `temp[6][2]' won't exist if temp was declared as `temp[10][2]'.

Looks to me like you need to overload the `[]' operator. You can do that like this:
Code:

```class Object{   int *arr;   int size; public:   Object():size(0),arr(0){}   Object(const int n):size(n){     arr = new int[n];   }   ~Object(){     if(arr){       delete [] arr;       arr = 0;     }   }   int& operator[](const int n){     if(n < size)       return arr[n];   } };```
So now you can see how to overload the operator, try adapting my code to suit your needs.

Note: I havn't tested this code. It was written straight into the forums text box.
• 03-25-2007
Daved
>> I had to overload the [] operator and I'm returning a pointer to that position in the array.
You should be returning a reference like tomcant's example does, not a pointer.
• 03-26-2007
warfang
I'll try returning a reference. I'm really confused about the whole reference vs pointer thing though. I'll try and figure it out. Thanks for the help.