data containers

Moritz mpipahl at gspgmbh.com
Fri Jul 4 13:06:22 PDT 2008


Im sorry, but I still get an ArrayBoundsError upon doing:
layer_map[layer] ~= new_element;

Same goes for:
auto single_layer = &layer_map[layer];
*single_layer ~= new_element;

And whats the point of doing:
ScreenElement[][] layer_map = new ScreenElement[][5];

Do I HAVE to initialise a number of empty arrays before concatenating 
instances of classes into it or not?
I dont see the point in giving some lenght in an dynamic array, for this 
would make it static, or am I wrong?

My whole add_element() function looks like this, just to give you all 
the info:

int add_element(ScreenElement new_element, uint layer)
{
	writefln("layer: %d", layer);
	writefln("layer_map.length: %d", layer_map.length);
		
	if(layer <= layer_map.length)
	{

		auto single_layer = &layer_map[layer];
	        *single_layer ~= new_element;
	
		//comented out, enalbed this instead of the upper two lines as an 
alternative
	        //layer_map[layer] ~= new_element;
	}
	else
	{
		writefln("cant insert ScreenElement into non existing layer");
	}
	return 0;
}


> No, it's because you're using a temp variable when you shouldn't be.
> 
> ScreenElement[] single_layer = layer_map[layer];
> single_layer ~= new_element;
> 
> That will get a reference the layer map at index `layer`, but when you 
> concatenate, only the local single_layer will refer to the newer, longer 
> array.  Just do it in place:
> 
> layer_map[layer] ~= new_element;
> 
> (Also if you don't feel like typing all those long types for variables, it's 
> completely unnecessary.
> 
> auto single_layer = layer_map[layer];
> 
> Dah.)



More information about the Digitalmars-d-learn mailing list