"not an lvalue"

Dmitry Olshansky dmitry.olsh at gmail.com
Sun May 1 06:53:14 PDT 2011


On 01.05.2011 17:30, CrypticMetaphor wrote:
> Hi, I've been away from D for a while, but now I'm back and I'm stuck 
> with an compile time error.
>
> I've got a Matrix33 class and a Vector3 class, but something is wrong 
> with the way I return my Vector3 in my matrix class:
>
> If I do this I get an error:
>
> Matrix33 mtest = new Matrix33();
> mtest.SetIdentity();
> Vector3 test1 = new Vector3(0, 0, 0);
> Vector3 test2 = test + mtest.GetColumn(2);
>
> I get the error "Error: mtest.GetColumn(x) is not an lvalue"
>
> But the following works:
>
> Matrix33 mtest = new Matrix33();
> mtest.SetIdentity();
> Vector3 test1 = new Vector3(0, 0, 0);
> Vector3 temp = mtest.GetColumn(2);
> Vector3 test2 = test + temp;
>
> // GetColumn method
> Matrix33
> {
> // ...
>
>   /// Get a matrix column, horizontal line
>   Vector3 GetColumn(uint index)
>   {
>     assert(!(index > 2), "index is too high");
>     return new Vector3(cell[index * 3], cell[index * 3 + 1], 
> cell[index * 3 + 2]);
>   }
> // ...
> }
>
> My questions:
> What changes do I have to make to make the first example compile?
Ehm.. Well, first things first: you shouldn't use classes for 
lightweight & plain data things like vectors. There are structs for 
that. In general, structs are value-like objects living on the stack 
while classes are reference-like objects living on the heap.  Your 
current code is going to allocate on GC heap new vector in every 
GetColumn and I suspect also when adding two vectors.

As for the error I  think the reason is in your's Vector's operator + 
overloading which expects ref Vector parameter it seems, though you'd 
better provide this function code as well.

-- 
Dmitry Olshansky



More information about the Digitalmars-d-learn mailing list