www.digitalmars.com         C & C++   DMDScript  

D - D newbie stumped: 'p.code.length is not an lvalue'

reply "Glenn M. Lewis" <noSpam noSpam.com> writes:
I have the following class:

class Program
{
   char[] code;
   int fitness;
   int index;
   int sortval;
}

At one point, I am trying to change the length of the 'code' array, like so:

static void crossover(Program p, Program p1)
{
   int n;
   int i;
//...
   // memcpy(p.code + i, p1.code + j, n);
   char[] tmp = p.code[j..(j+n)];
   p.code.length = i + n;   // COMPILER ERROR
   p.code[i..(i+n)] = tmp[];
}

I don't understand why I get the error message: 'p.code.length is not an
lvalue'...
I thought I could change the length of the array?

Thanks!
								-- Glenn Lewis
Mar 12 2004
next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Glenn M. Lewis wrote:

[...]
 I get the error message: 'p.code.length is not an lvalue'...

Not confirmed. Your example complies okay on WIN98SE, dmd 0.81. So long.
Mar 12 2004
prev sibling parent J C Calvarese <jcc7 cox.net> writes:
Glenn M. Lewis wrote:
 I have the following class:

I had to add some lines to get it to compile. (You didn't give us a compilable example to work with.) Here's a complete example that does compile: class Program { char[] code; int fitness; int index; int sortval; } static void crossover(Program p, Program p1) { int n; int i; int j; //... // memcpy(p.code + i, p1.code + j, n); char[] tmp = p.code[j..(j+n)]; p.code.length = i + n; // COMPILER ERROR p.code[i..(i+n)] = tmp[]; } void main() { Program myP = new Program(); Program myP1 = new Program(); crossover(myP, myP1); }
 
 class Program
 {
   char[] code;
   int fitness;
   int index;
   int sortval;
 }
 
 At one point, I am trying to change the length of the 'code' array, like 
 so:
 
 static void crossover(Program p, Program p1)
 {
   int n;
   int i;
 //...

I'm going to go out of a limb here and guess this is where the error is. I'm going to go even farther out on a limb and guess you have something like this in there: p.code.length++; If so, you'll have to change it to: p.code.length = p.code.length + 1; If none of this helps, you might try posting a fuller example. I can't find anything wrong with the code you've posted. If I could duplicate your error, I might be able to fumble around into a solution.
   // memcpy(p.code + i, p1.code + j, n);
   char[] tmp = p.code[j..(j+n)];
   p.code.length = i + n;   // COMPILER ERROR
   p.code[i..(i+n)] = tmp[];
 }
 
 I don't understand why I get the error message: 'p.code.length is not an 
 lvalue'...
 I thought I could change the length of the array?
 
 Thanks!
                                 -- Glenn Lewis

-- Justin http://jcc_7.tripod.com/d/
Mar 12 2004