Error: xxx is not an lvalue

Tyro[a.c.edwards] nospam at home.com
Sat May 2 14:19:38 PDT 2009


On 5/3/2009 3:44 AM, flourish wrote:
> Hi,
>
> why does the following code not compile -- or how to declare a (usable)
> property of an associative array field?
>
> //////////////////////
> class Test
> {
>    private int[int] _testMap;
>    public int[int] testMap() {return _testMap;}
      public void opIndexAssign(int val, int ndx){ _testMap[ndx] = val; }
> }
>

testMap() is a getter and can only return the value it is designed to.
You must implement a setter to accomplish what you are trying to do.
Unfortunately I'm not sure how to do it with AAs so I'll leave the
details to someone that's a little smarter than I.

I think that maybe implementing opIndexAssign might do the trick though.
See modifications to Test and main().

> void main()
> {
>    Test test = new Test();
      //test.testMap[0] = 1; // Error: getters cannot be assigned to
      test[0] = 1;
> }
> /////////////////
>
> *** Error: test.testMap() is not an lvalue
>
>
> Regards,
>      flourish
>
>
>




More information about the Digitalmars-d mailing list